一.重复输出字符串

print('hello'*20)#输出20个hello

二.通过索引获取字符串中字符

print('helloworld'[2:])#输出lloworld

三.关键字 in

print('ll' in 'hello')#输出True

四.格式化输出

print('Darling,I love you')
print('%s,I love you'%'Darling')

五.字符串的连接

a='123'
b='abc'
d='44'
c= ''.join([a,b,d])
print(c)#输出123abc44
c= '*'.join([a,b,d])
print(c)#输出123*abc*44

六.字符串的内置方法

str='Darling,I love you'
print(str.count('l'))       #  统计元素'l'的个数
print(str.capitalize())     #  只有首字母大写
print(str.center(50,'#'))   #  居中###############Darling,I love you################
print(str.endswith('you')) #  判断是否以某个内容结尾
print(str.startswith('darling')) #  判断是否以某个内容开头,此处输出False
print(str.find('i')) #  查找到第一个元素,并将索引值返回,如果没有该元素输出-1
print(str.index('a'))#查找到第一个元素,并将索引值返回,如果没有该元素则报错
print('{name} is {age}'.format(name='sfencs',age=19))  # 格式化输出的另一种方式sfencs is 19
print('{name} is {age}'.format_map({'name':'sfencs','age':19}))
print('Dartling,I love you'.expandtabs(tabsize=20))#制表符的长度为20
print('asd'.isalnum())#检测字符串是否由字母和数字组成
print('12632178'.isdecimal())#检查字符串是否只包含十进制字符
print('1269999'.isnumeric())#检测字符串是否只由数字组成
print('abc'.isidentifier())#判断是否满足标识符定义规则。只能是字母或下划线开头、不能包含除数字、字母和下划线以外的任意字符。
print('Abc'.islower())#检测字符串是否全由小写字母组成
print('ABC'.isupper())#检测字符串是否全由大写字母组成
print('  e'.isspace())#检测字符串是否只由空格组成
print('My title'.istitle())#检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写
print('My tItle'.lower())#转换字符串中所有大写字符为小写
print('My tItle'.upper())#转换字符串中所有小写字符为大写
print('My tItle'.swapcase())#对字符串的大小写字母进行转换
print('My tItle'.ljust(10,'*'))#返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串My tItle**
print('My tItle'.rjust(10,'*'))#返回一个原字符串右对齐,并使用空格填充至指定长度的新字符串**My tItle
print('tMy tLtlen'.strip())#用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
print('tMy tLtlen'.lstrip())#用于截掉字符串左边的空格或指定字符
print('tMy tLtlen'.rstrip())#用于截掉字符串右边的空格或指定字符
print('My title title'.replace('title','new',1))#把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
print('My title title'.rfind('t'))#从右向左寻找第一个t的索引
print('My title title'.split('i',1))#通过指定分隔符对字符串进行切片,数字参数为分割的次数,不填为全分割
print('My title title'.title())#返回'标题化'的字符串,即所有单词都是以大写开始,其余字母均为小写

  

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!