python之字符串学习(一)

学习Python语言,不得不学习在Python中,对字符串的处理,

我们可以看到在str类中,提供了很多对字符串的操作的方法,我们现在需要做的,就是把经常使用到

的方法在这里进行下总结和学习。具体见如下的代码:

#!/usr/bin/env python 
#coding:utf-8

str='Hello'
#首字母变大写
print str.capitalize()
#内容居中
print str.center(30,'=')
#子序列的个数(字母在字符串中出现了几次)
print str.count('l')
#是否已什么结尾
print str.endswith('o')
#是否已什么开始
print str.startswith('H')
#处理tab键
str2='Hello\t999'
print str2.expandtabs()
#寻找子序列位置,没有找到返回-1,返回了是1
print str.find('a')
#寻找子序列的位置,没有找到就报错
print str.index('e')
#字符串的格式化输出,也就是占位符
age=20
name='hello'
print 'name is {0},and age is {1}'.format(name,age)
#判断是否是字母和数字
print str.isalnum()
#判断是否是字母
print str.isalpha()
#判断是否是数字
print str.isdigit()
#判断是否小写
print str.islower()
#判断是否有空格
print str.isspace()
#判断是否是大写
print str.isupper()
#判断是否是标题-->首字母大写,可以理解为大写
print str.istitle()
#.join连接字符串
s1=['appium','selenium','android','ios']
print '***'.join(s1)
#使用.join()把列表转为字符串
print ','.join(s1)
#字符串转为列表
a='a b c'
print a.split(' ')
#移除空白
s3=' hello'
print s3.strip()
#移除左側空格
s4=' hello'
print s4.lstrip()
#移除右边空格
s5='world '
print s5.rstrip()
#字符串变小写
print str.lower()
#分割字符串,分割后就是元组
s='wuya is python'
print s.partition('is')
#替换字符串
print s.replace('wuya','selenium')
#rfind()从右向左找
print s.rfind('wuya')
#bytes可以把字符串转成字节
str5='你好'
print bytes(str5)

猜你喜欢

转载自www.cnblogs.com/qingbaobei7370/p/10971830.html