#Join 方法,Join方法是非常重要的字符串方法,它是split方法的逆向方法,用于连接序列中的元素
set = ['1','2','3','4','5']#用于连接字符串。
sep = '+'
sep.join(set)
dirs = '','usr','bin','env'
'/'.join(dirs)
print 'C:' + '\\'.join(dirs)
#C:\usr\bin\env
# lower方法,lower方法返回字符串的小写字母版。另外此方法还具有忽略大小写功能,模糊搜索将用到。
'Hello World.'.lower()
#这是一个实例,如果用户输入的字符串中含有text里的元素则返回真,注:’本方法仅对小写有效。‘
name = raw_input('输入一个字符:')
text = ['And','bnc','Unic','hello']
if name.lower() in text:
print "That's the True"
else:
print "That's the Noting."
#title 方法,title方法可将字符串转换为标题,即所有单词首写字母大写,其他小写。
"that's a example".title()
#这是一个实例,当用户输入字符后将自动转换成标题形式。
tx = raw_input('请输入标题:')
print tx.title()
#类似title方法的还有另一种实现方式:
import string
print string.capwords("that's a example")
#replace方法,replace方法可用于替换字符串中的字符。
print "This 's the new example.".replace('example','string')
#这是一个实例,用户可以自定义替换字符串中的内容。
show = "This's the new example."
print show
inp = raw_input('请输入您想要替换的字符:')
tr = raw_input('您想要将%s替换成什么呢?:'%(inp))
print "这是替换后的效果:" + show.replace(inp,tr)
#split方法,split是一个非常重要的字符串方法,它是join的逆向方法,常用于字符分割。
print '1+2+3+4+5'.split('+')
print 'C:/User/acer/Desktop'.split('/')
print "This's the New Day.".split()
#这是一个实例,用户可以自定义切割字符串中的内容。
show = "This's + the + New + Example,But,The + Example + is + only + for + you"
print show
inp = raw_input('您想要如何分割文本呢?:')
print show.split('%s'% (inp))
'''''''''''
'''''''''''
#strip方法,strip会去除两侧(不包含内部)空格的字符
print " This's is the New Example.but,the Example is only for you. ".strip()
#这是一个实例,用户可以自定义替换字符中的内容、切割文本、去除多余的空格以及内容添加,本实例将以while循环遍历实现。
show = " This's +is +the +New+Example.but+the+example+is+only+for+you. "
print show
while True:
inp = raw_input('您是想(Q.切割文本/T.替换内容/D.去空/A.添加内容):')
if inp == 'Q':
cut = raw_input('您想以哪种方式进行分割呢?:')
print show.split(cut)
break
elif inp == 'T':
cut = raw_input('您想要替换哪部分内容呢?:')
put = raw_input('您想将%s替换成什么呢?:'% cut)
print show.replace(cut,put)
break
elif inp == 'D':
print show.strip()
break
elif inp == 'A':
cut = raw_input('请输入您要往字符串中添加的内容:')
print show.join(cut)
break
else:
print '您的输入有误,请重新输入!'
0 评论:
发表评论