字符串输出:
name = 'python-china'
1,直接输出:print(a) # python-china
2,格式化输出:print('本站是%s' % s) #本站是python-china
3,f-strings输出:f-strings 提供一种简洁易读的方式, 可以在字符串中包含 Python 表达式. f-strings 以字母'f' 或 'F' 为前缀, 格式化字符串使用一对单引号、双引号、三单引号、三双引号. 格式化字符串中
name = 'bayi'
age = 35
format_string1 = f'我的名字是 {name}, 我的年龄是 {age}'
format_string2 = f"我的名字是 {name}, 我的年龄是 {age}"
format_string3 = F'''我的名字是 {name}, 我的年龄是 {age}'''
format_string4 = F"""我的名字是 {name}, 我的年龄是 {age}"""
format_string5 = f'3 + 5 = {3 + 5}'
a = 10
b = 20
format_string6 = f'3 + 5 = {a + b}'
# 两个花括号会被替换为一个花括号, 注意{{}} 不表示表达式
format_string7 = F'我的名字是 {{name}}, 我的年龄是 {{age}}'
** f-strings在Python3.6版中提供,查看python版本命令:python --version ***
字符串常见操作:
如有字符串mystr = 'hello world python and pythonchina',以下是常见的操作:
1,find:检测str是否包含在mystr中,如果是返回开始的索引值,否则返回-1。rfind从右边开始查找
格式:mystr.find(str, start=0, end=len(mystr)
print(mystr.find('python')) # 结果是下标索引值12,空格也算一个字符
print(mystr.find('python', 0, 11)) # 结果不存在,反回-1
2,index,跟find()方法一样,只不过如果str不在 mystr中会报一个异常。rindex从右边开始查找
print(mystr.index('python', 0, 11)) # 结果不存在,程序报错
3,count:返回 str在start和end之间 在 mystr里面出现的次数
mystr.count(str, start=0, end=len(mystr))
print(mystr.count('python')) # 结果为2,表示出现2次
4,replace:把mystr中的str1替换成str2,如果count指定,则替换不超过count次.
replace格式:mystr.replace(str1, str2, mystr.count(str1))
print(mystr.replace('python','php')) # 结果:hello world php and phpchina
print(mystr.replace('python','php',1)) # 只替换一次,结果:hello world php and pythonchina
5,split:以str为分隔符切片mystr,如果maxsplit有指定值,则仅分隔maxsplit个子字符串,返回的是一个列表
split格式:mystr.split(str="str", maxsplit=maxsplit)
print(mystr.split(' ')) # 以空格切割整个字符串,结果:['hello', 'world', 'python', 'and', 'pythonchina']
print(mystr.split(' ', 2)) # 以空格切割整个字符串,只切割两次,结果:['hello', 'world', 'python and pythonchina']
6,join:mystr 中每个元素后面插入str,构造出一个新的字符串
join格式:str.join(mystr) # 前面插入到后面字符串
print('_'.join('hello')) # 把1插入到hello的每个元素后面,输出结果为:h_e_l_l_o
print('_'.join(['hello', 'world', 'hello', 'python'])) # 输出结果为:hello_world_hello_python
7,capitalize:把字符串的第一个字符大写。
print(mystr.capitalize()) # 输出结果为:Hello world python and pythonchina
8,title:把字符串的每个单词首字母大写
print(mystr.title()) # 输出结果为:Hello World Python And Pythonchina
9,startswith:检查字符串是否是以str开头, 是则返回True,否则返回False,相对应的是endswith,以str结尾
print(mystr.startswith('hello')) # 结果为True
print(mystr.endswith('hello')) # 结果为False
10,lower:转换 mystr 中所有大写字符为小写。对应的是upper,把全部字符转换成大写
mystr = 'Hello Python'
print(mystr.lower()) # 结果hello python
print(mystr.upper()) # 结果HELLO PYTHON
11,ljust:返回一个原字符串左对齐,并使用空格填充至字符长度width的新字符串。对应的是rjust,center
mystr = 'Hello Python'
print(mystr.ljust(20)) # 结果hello python
print(mystr.rjust(20)) # 结果 hello python
print(mystr.center(20)) # 结果 hello python
12,strip:删除mystr头尾空白字符,对应的(左)lstrip,对应的(右)rstrip
mystr = ' hello '
print(mystr.strip()) #结果:hello
print(mystr.lstrip()) #结果:hello
print(mystr.rstrip()) #结果: hello
13,partition:把mystr以str分割成三部分,str前,str和str后,返回一个元组。对应的是rpartition
mystr = 'hello world hello china'
print(mystr.partition('world')) # 结果('hello ', 'world', ' hello china')
print(mystr.rpartition('hello')) # 结果('hello world ', 'hello', ' china')
14,splitlines:按照行分隔,返回一个包含各行作为元素的列表
mystr = 'hello world\nhello china'
print(mystr.splitlines()) # 结果:['hello world', 'hello china']
15,isalpha:判断mystr是否都是字母,是则返回 True,否则返回 False
mystr = 'hello world'
print(mystr.isalpha()) # 有空格,结果:False
16,isdigit:判断mystr是否都是数字 是则返回 True,否则返回 False
mystr = '888'
print(mystr.isdigit()) # 结果:True
17,isalnum:判断mystr是否都是数字或者,是则返回 True,否则返回 False
mystr = '888abc'
print(mystr.isalnum()) # 结果:True
18,isspace:判断mystr是否只包含空格,是则返回 True,否则返回 False
mystr = ' '
print(mystr.isspace()) # 结果:True
PS:字符串还有很多操作,这里只列了常用的18种操作,有兴趣的可以自己去查找更多的字符串操作相关资料