字符串操作在python3-基础篇-04-字符串格式化输出(%、format())中已经提到了一些,在本章中将列举字符串的其它操作。

1.字符串重复输出

‘值’*num     (num为重复输出的次数)

print('lvyq'*2)

运行结果

lvyqlvyq


2通过索引获取字符串

'值'[start_index:end_index]    (start_index 开始下标  end_index 结束下标 ,遵循左包含右不包含)

print("lvyq"[0:2])

运行结果

lv


3.判断字符串是否包含

'值1' in '值2'    

print('lv' in 'lvyq')

运行结果

True


4.字符串拼接

1. +          占用内存多 

2.jion(推荐使用)

# + 
str1= 'hello'
str2 = 'lvyq'
print(str1+str2)
# 'join'
print(''.join([str1,str2]))
print('**'.join([str1,str2]))

运行结果

hellolvyq
hellolvyq
hello**lvyq


5.内置方法 (红色为常用方法)

方法

说明

示例            

结果

str.count('value')

统计元素个数

print('hellolvyq'.count('l'))


3

 

str.capitalize()

首字母大写

print('hello lvyq'.capitalize())


Hello lvyq

str.center(num,'*')

居中

print('lvyq'.center(8,'*'))
print('lvyq'.center(8,' '))


python连续三个相同字符 python字符串重复三次并输出_python连续三个相同字符

str.endswith('value')

判断是否以某个字符串结尾 True、False

print('lvyq'.endswith('q'))


True

str.startswith('value')

判断是否以某个字符串开头 True、False

print('lvyq'.startswith('q'))


False

str.expandtabs(tabsize=num)

#设置字符串中\t的格数

print('lv\tyq'.expandtabs(tabsize=10))


python连续三个相同字符 python字符串重复三次并输出_字符串_02

str.find('value')

查找到第一元素,并将索引值返回

print('lvyq'.find('l'))


0

str.format()

格式化输出

str.format_map({})

格式化输出

str = "my name is: {name}"
print(str.format_map({'name':'lvyq'}))


python连续三个相同字符 python字符串重复三次并输出_python连续三个相同字符_03

str.index('value')

查找到第一元素,并将索引值返回,与find()作用一样

print('lvyq'.index('l'))


0

str.isalnum()

检测字符串是否由字母和数字组成

print('my age is 12!'.isalnum())
print('age12'.isalnum())


False

True

str.isdigit()

判断字符串是否是整型

print('12'.isdigit())
print('12.1'.isdigit())


True

False

str.isidentifier()

判断是否是有效标识符

print('abc'.isidentifier())
print('a c'.isidentifier())


True

False

str.islower()

判断字符串是否全是小写

print('Lvyq'.islower())
print('lvyq'.islower())


False

True

str.isupper()

判断字符串是否全是大写

print('LVYQ'.isupper())
print('lvyq'.isupper())


True

False

str.isspace()

判断字符串是否只由空白字符组成

print('  '.isspace())
print(' lvyq'.isspace())


True

False

str.istitle()

检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

print('My Name'.istitle())
print('My name'.istitle())


True

False

 

str.lower()

将字符串的大写字母改为小写

print('My Name'.lower())


my name

str.upper()

将字符串的小写字母改为大写

print('My Name'.upper())


MY NAME

str.swapcase()

将字符串的大写字母改为小写,小写字母改为大写

print('My Name'.swapcase())


mY nAME

str.ljust(num,' ')

返回一个原字符串左对齐,并使用指定的字符串(默认空格)填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串,

print('My Name'.ljust(10,"*"))


python连续三个相同字符 python字符串重复三次并输出_字符串_04

str.rjust(num,' ')

返回一个原字符串右对齐,并使用指定的字符串(默认空格)填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串,

print('My Name'.rjust(10,"*"))


python连续三个相同字符 python字符串重复三次并输出_python_05

str.strip()

去除字符串两端的空格

print('  My Name'.strip())


python连续三个相同字符 python字符串重复三次并输出_ide_06

str.lstrip()

去除字符串左边的空格

print(' My Name '.lstrip())


python连续三个相同字符 python字符串重复三次并输出_python连续三个相同字符_07

str.rstrip()

去除字符串右边的空格

print(' My Name '.rstrip())

python连续三个相同字符 python字符串重复三次并输出_python连续三个相同字符_08

str.replace(oldchart,newchart)

替换字符串,当存在多个相同的字符串时还可以指定替换的数量

print('My Name'.replace('Title','AAA'))
print('My Name Name'.replace('Name','AAA'))
print('My Name Name'.replace('Name','AAA',1))


python连续三个相同字符 python字符串重复三次并输出_格式化输出_09

str.rfind('value')

查找字符位置并返回最后边的索引值

print('MyNameName'.rfind('a'))

7

str.split('value')

str.split('value',num)

根据字符分割。可以指定根据字符分割的次数

print('MyNameName'.split('a'))
print('MyNameName'.split('a',1))


python连续三个相同字符 python字符串重复三次并输出_格式化输出_10

str.title()

返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写

print('My Name Name'.title())
print('MyName Name'.title())


python连续三个相同字符 python字符串重复三次并输出_python_11