2.Python字符串基础

split,join,strip,replace,lower,upper,capitalize

1.字符串。

print("{0:s}".format('I\'m enjoying learning python.')) #双引号包含,不需要反斜杠。'
print("{0:s}".format("I'm enjoying learning python."))
print("{0:s}".format('''you can use triple single quotes
for multi-line comment strings.'''));   #这个会自然换行。
print("{0:s}".format("""you can use triple single quotes
for multi-line comment strings."""));   #这个会自然换行。


I'm enjoying learning python.
I'm enjoying learning python.
you can use triple single quotes
for multi-line comment strings.
you can use triple single quotes
for multi-line comment strings. 

#当使用3个单引号或这三个双引号则不需要使用反斜杠。

string1="This is a "
string2="short string."
sentence=string1+string2
print("{0:s}".format(sentence))
print("{0:s}{1:s}{2:s}".format("She is ","very "*4,"beautiful."))

This is a short string.
She is very very very very beautiful.

m=len(sentence)
print("{0:d}".format(m))

+:可以将字符串相加。
*:操作符可以将字符串重复一定的次数。

7.split 函数
#split 函数:
split 函数可以再括号中使用两个附加参数。
第一个附加参数表示使用哪个字符进行拆分
第二个附加参数表示进行拆分的次数。

string1="My deliverable is due in May"
string1_list1=string1.split()
string1_list2=string1.split(" ",2)  #只想对前两个空格做拆分,后面的不做拆分。
print("{0}".format(string1_list1))
print("{0}".format(string1_list2)) 
print("First price:{0} Second price:{1} Third price:{2}".format(string1_list2[0],string1_list2[1],string1_list2[2]))
string2="Your,deliverable,is,due,in,June"
string2_list=string2.split(',')
print("{0}".format(string2_list))
print("{0} {1} {2}".format(string2_list[1],string2_list[5],string2_list[-1]))

#['My', 'deliverable', 'is', 'due', 'in', 'May']
#['My', 'deliverable', 'is due in May']
#First price:My Second price:deliverable Third price:is due in May
#['Your', 'deliverable', 'is', 'due', 'in', 'June']
#deliverable June June

#没有附加参数,所以split函数使用空格字符(默认值)对字符串进行拆分。
#附加参数是个逗号,split函数在出现逗号的位置拆分字符串。

2.join 
print("{0}".format(','.join(string2_list)))
#Your,deliverable,is,due,in,June
#使用逗号将集合中的每个字符串组成一个字符串。

3.strip 
strip,lstrip,rstrip 
#从字符串删除不想要的字符。

string3=" Remove unwanted characters from this string.\t\t \n"
print("string3:{0:s}".format(string3))
string3_lstrip=string3.lstrip()
print("lstrip:{0:s}".format(string3_lstrip))
string3_rstrip=string3.rstrip()
print("rstip:{0:s}".format(string3_rstrip))
string3_strip=string3.strip()
print("strip:{0:s}".format(string3_strip))


#string3: Remove unwanted characters from this string.        #原样输出。 
#lstrip:Remove unwanted characters from this string.    #删除左边空格。     
#rstip: Remove unwanted characters from this string.    #删除右边空格。
#strip:Remove unwanted characters from this string.     #删除两个空格。


string4="$$Here's another string that has unwanted characters.__---++"
print("{0:s}".format(string4))
string4_strip=string4.strip('$_-+')
print("{0:s}".format(string4_strip))

#$$Here's another string that has unwanted characters.__---++
#Here's another string that has unwanted characters.
#strip删除字符串两端的:$,_,-,+ 四种字符。

4.replace 
#第一个字符是字符串中查找要替换的字符或字符串
#第二个位置是新的字符或字符串。

 

string5="Let's replace the spaces in this sentence with other characters."
string5_replace=string5.replace(" ","!@!")   #将空格替换为"!@!"
print("{0:s}".format(string5_replace))
string5_replace=string5.replace(" ",",")     #将空格替换为逗号。
print("{0:s}".format(string5_replace))
#Let's!@!replace!@!the!@!spaces!@!in!@!this!@!sentence!@!with!@!other!@!characters.
#Let's,replace,the,spaces,in,this,sentence,with,other,characters.

5.lower,upper,capitalize

string6="Here's WHAT Happens WHEN You Use lower."
print("{0:s}".format(string6.lower()))
print("{0:s}".format(string6.upper()))
print("{0:s}".format(string6.capitalize())) #字符串第一个字母大写,其他小写。
string5="Let's replace the spaces in this sentence with other characters."
string5_list=string5.split()
for word in string5_list:
    print("{0:s}".format(word.capitalize()))


here's what happens when you use lower.
HERE'S WHAT HAPPENS WHEN YOU USE LOWER.
Here's what happens when you use lower.
Let's
Replace
The
Spaces
In
This
Sentence
With
Other
Characters.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值