字符串操作

#Author:Kevin_hou
#字符串操作
name =" My name is kevin.hou and I am 30 years old"
name1="123456"
print(name.__add__(' !!!'))#在字符串后面添加值
print(name.count('a'))  #计算字符串中a的数量
print(name.center( 50,'*')) #使字符串居中,左右添加*

print(name.endswith('30'))  #判断是否已字符串结束,不是返回False
print(name.endswith('old')) #是,返回True

print(name.format(name='kevin.hou',year =30))   #
print(name.find('I'))   #查找字符串’I‘,输出位置值

print('my name is'.istitle())   #首字母小写判定为不是标题,输出False
print('My Name Is'.istitle())   #首字母大写判定为标题,输出为True

print('abc'.islower())  #判断是否为小写;是,True;非,False
print('ABC'.islower())  #False
print('321'.islower()) #False

print(name.isdigit())   #判断name是否为数字变量,不是,则返回False;是,则返回True
print(name1.isdigit( )) #

print('abc123'.isalnum())   #判断字符串变量是否为字母或者数字;是,输出True;非,输出False
print(',*_<'.isalnum())
print('a*_<'.isalnum())

print('a 2B'.isidentifier())    #判断是否为1个合法标识符;False
print('a_2B'.isidentifier())    #True
print('a2B'.isidentifier()) #True
print('a'.isidentifier())   #True

print(','.join(['1','2','3']))  #在字符串输出间添加逗号;输出为1,2,3
print(name.ljust(50,'-'))   #输出:My name is kevin.hou and I am 30 years old-------
print(name.rjust(50,'*'))   #输出: ******* My name is kevin.hou and I am 30 years old

print('KEVIN'.lower())  #大写变小写;输出:kevin
print('kevin'.upper())  #小写变大写;输出:KEVIN

print('kevin\n'.lstrip())   #省去中间输出的换行
print('kevin'.lstrip())

print('KEVIN.HOU'.rfind('o'))   #在字符串中找到'o';找到,输出数组值;找不到,输出-1。输出:-1
print('KEVIN.HOU'.rfind('x'))   #输出:-1
print('KEVIN.HOU'.rfind('O'))   #输出:7

print('KEVIN.HOU'.replace('KEVIN','kevin',5)) #替换值;输出:kevin.HOU
print('1+2+3'.split('+'))   #按照加号分割。输出:['1','2','3']

  

posted @ 2020-07-24 23:00  JRS077  阅读(66)  评论(0编辑  收藏  举报