cl_303303303 发表于 2018-8-13 09:10:42

python3 字符串操作

#大写  

  
print(name.capitalize()) # 首字母大写
  

  
打印显示
  
My name is{name} and my age is {year} old
  

  

  
#统计
  
print(name.count("a")) # 统计 a 的个数
  

  
#打印显示
  
5
  

  
#中间补齐
  
print(name.center(50,"#"))
  

  
#打印显示
  
###My name is{name} and my age is {year} old###
  

  
#判断字符串以什么结尾,正确为true ,错误为false
  

  
print(name.endswith("ex"))
  

  
#打印显示
  
False
  

  
#tab 健补全
  

  
print(name.expandtabs(tabsize=10)) #10 表示10个字符
  

  
#查找字符索引
  

  
print(name.find("M"))
  

  
#打印显示
  

  
0
  

  
#format 格式化
  

  
print(name.format(name='bob',year=33))
  

  
print(name.format_map({'name':'jerrt','year':27}))
  

  
#打印显示
  
My name isbob and my age is 33 old
  
My name isjerrt and my age is 27 old
  

  

  
#如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
  

  
print('a31'.isalnum())
  

  

  
#打印显示
  

  
True
  

  
#判断是否为纯英文字符
  

  
print('abA'.isalpha())
  

  
print('abA1'.isalpha())
  

  

  
#打印显示
  

  
True
  
False
  

  
#判断是否为10进制
  

  
print('1A'.isdecimal())
  
print('113'.isdecimal())
  
#打印显示
  
False
  
True
  

  
#检测字符串是否只由数字组成
  
print('111'.isdigit())
  
print('1AA'.isdigit())
  

  
#打印显示
  
True
  
False
  

  
#判断是否为合法的标识符
  
print('1A'.isidentifier())
  

  
print('_1A'.isidentifier())
  

  
False
  
True
  

  
#方法检测字符串是否只由数字组成。这种方法是只针对unicode对象
  

  
print('a AA'.isnumeric())
  

  
print('11'.isnumeric())
  

  
#打印显示
  
False
  
True
  

  
#检测字符串是否只由空格组成
  
print('ssA'.isspace())
  
print('ssA'.isspace())
  
print(''.isspace())
  

  
#打印显示
  
False
  
False
  
True
  

  
#判断字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False.
  

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

  
#打印显示
  
False
  
True
  

  
#检测字符串中所有的字母是否都为大写
  

  
print('MY NAME'.isupper())
  
print('My Name is'.isupper())
  
#打印显示
  
True
  
False
  

  
#join 方法 用于将序列中的元素以指定的字符连接生成一个新的字符串
  

  
print("+".join(['a1','b2','c3']))
  

  
#打印显示
  
a1+b2+c3
  

  
#ljust 返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
  
name = name = "My \tname is{name} and my age is {year} old"
  
print(name.ljust(50,"*"))
  
#打印显示
  
My name is{name} and my age is {year} old******
  

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

  
print(name.rjust(50,"*"))
  

  
******My name is{name} and my age is {year} old
  

  
#lower大写变小写
  

  
print('BAG'.lower())
  

  
#打印显示
  

  
bag
  

  
#upper 小写变成大写
  

  
print('bob'.upper())
  

  
#打印显示
  

  
BOB
  

  
#用于截掉字符串左边的空格或指定字符
  

  
print('\nAlex'.lstrip('n')) #从左边去空格
  
#打印显示
  
Alex
  

  
print('Alex\n'.rstrip('\n')) #从右边去空格
  

  
#打印显示
  
Alex
  

  
#strip 用于移除字符串头尾指定的字符(默认为空格)
  
print('      Alex\n'.strip()) #去空格
  
#打印显示
  
Alex
  

  
#replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max
  

  
print('Bobbb'.replace('b','B',2))
  

  
print('bob'.replace('b','B'))
  

  
#打印显示
  
BoBBb
  
BoB
  

  
#split 通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串
  

  
print('ljack lex lbob ltim '.split('l'))
  

  
print('1+2+3+4'.split('+')) #按照+ 区分
  

  
#打印显示
  
['', 'jack ', 'ex ', 'bob ', 'tim ']
  
['1', '2', '3', '4']
  

  
#title 标题
  
print('hi world'.title())
  

  
#打印显示
  
Hi World
  

  
#zfill 自动补位 方法返回指定长度的字符串,原字符串右对齐,前面填充0
  
print('lex li'.zfill(10))
  

  
#打印显示
  

  
0000lex li
页: [1]
查看完整版本: python3 字符串操作