当前位置: 移动技术网 > IT编程>脚本编程>Python > python 3.0 字符串的内置方法

python 3.0 字符串的内置方法

2018年05月02日  | 移动技术网IT编程  | 我要评论

中新网首页,徐云潇,中国气功在线

#String的内置方法

st='hello world'

★1、print(st.count('l'))  #统计元素在字符串的个数

#==> 3

2、print(st.capitalize()) #首字母大写

#==> Hello world

★3、print(st.center(50,'-'))  #字符串居中

#==> -------------------------hello world-------------------------

4、print(st.endwith('rld')) #判断结尾元素

#==>True

★5、print(st.startswith('hello')) #判断开头元素

#==>True

6、st='hello w\torld'

print(st.expandtabs(tabsize = 10))

#==> hello w          orld

★7、st='hello w\torld'

print(st.find('t')) #查找到第一个元素并将其索引值返回

#==> 8

★8、st='hello world {name} is {age}'   #格式化输出的另一种方式

print(st.format(name ='karen',age=22))

#==> hello world karen is 22

9、st='hello world {name} is {age}'   #格式化输出的另一种方式

print(st.format_map({'name':'karen','age':22}))

#==> hello world karen is 22

10、print(st.index('h'))  #和find用法一样,但如果找不到就报错

#==> 0

11、print(st.isalnum())  #字符串是否只包括数字和字母

12、print(st.isdecimal())  #是不是十进制的数

13、print(st.isdigit()) #是不是整型数字 

14、 print(st.isnumeric())    #14=13

15、print(st.isdentifier())  #判断是不是一个非法字符    变量   :数字不能开头

16、print(st.islower())  #是不是全部小写

17、print(st.isupper())  #是不是全部大写

18、print(st.isspace())  #是不是全部是空格

19、print(st.istitle())  #是不是一个首字母是大写

★20、print(st.lower())  #大写变小写

★21、print(st.upper()) #小写变大写

★22、print(st.swapcase()) #大写变小写、小写变大写

23、print(st.ljust(50,'-')) #左边补充

24、print(st.lrjust(50,'-'))#右边补充

25、print(c='------'.just([a,b ,c]))#用连接符将字符串连接

★26、print(st.strip())  #去掉前后空格、换行符\n、制表符\t

27、print(lst.lstrip())

28、print(st.rstrip())

★29、print(st.replace('hello','world')) #字符串中的所有与替换的元素相同的元素全部替换

#==> world world

30、print(st.rfind('l')) #最右面要查找的字符的索引

#==>9

★★★31、print(st.sprit(' ',1))  #以什么作为分隔符把字符串整合成一个列表

32、print(st.rsprit(' ',1))   #以右为准分割几次输出

33、print(st.title(''))  #首字母全部大写

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网