|
作者:Sejal Jaiswal
日期:January 18th, 2018
主要内容包括slicing、striding、
字符串不可变类型 |
single_quote = 'Single quote allow you to embed "double" quotes in your string.'
double_quote = "Double quote allow you to embed 'single' quotes in your string."
triple_quote = """Triple quotes allows to embed "double quotes" as well as 'single quotes' in your string.
And can also span across multiple lines."""
triple_quote = '''This is triple quoted string using "single" quotes.'''
triple_quote[35] = "'"
#
#result
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-207-62d335428dcf> in <module>()
1 triple_quote = '''This is triple quoted string using "single" quotes.'''
----> 2 triple_quote[35] = "'"
TypeError: 'str' object does not support item assignment
triple_quote_new = triple_quote[0:35] + "'single'" + triple_quote[43:]
print(triple_quote_new)
#
#
This is triple quoted string using 'single' quotes.
可以用
len()计算字符串的长度
len(triple_quote_new)
#
#
51
字符串切片 |
由于字符串是由一连串的字母组成的,因此,你可以像元组和列表那样将字符串切片或索引。
#索引
snack = "Chocolate cookie."
print(snack[0])
print(snack[9])
print(snack[-1])
#
#
C
.
#切片
snack = "Chocolate cookie."
print(snack[10:16])
#
#
cookie
#与上面等价,你也可以采用负数索引
print(snack[10:-1]) # -1: since the stop index is excluded in slicing.
#
#
cookie
下面三个例子的结果是一样的:
# Stop value not provided
print(snack[0:])
# Start value not provided (Stop value excluded according to syntax)
print(snack[:-1])
# This is also allowed
print(snack[:])
#
#
Chocolate cookie.
Chocolate cookie
Chocolate cookie.
下面看看字符串中的stride:
number_string = "1020304050"
print(number_string[0:-1:2])
#
#
12345
print(number_string[::-1])
#
#
0504030201
print(number_string[::-2])
#
#
00000
常用字符串操作 |
#连接
string1 = 'Chocolate'
string2 = 'cookie'
snack = string1 + " " + string2
print(snack)
#
#
Chocolate cookie
然而,字符串与数字进行连接时,数字为数值型是会报错的:
cost = 15
string1 = "The total in Euro is: "
bill = string1 + cost
print(bill)
#
#
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-218-7d5c5248b927> in <module>()
2 string1 = "The total in Euro is: "
3
----> 4 bill = string1 + cost
5 print(bill)
TypeError: Can't convert 'int' object to str implicitly
如下操作就可以:
bill = string1 + str(cost)
print(bill)
#
#
The total in Euro is: 15
- 复制字符串可以用 * 操作:
single_word = 'hip '
line1 = single_word * 2 + 'hurray! '
print(line1 * 3)
#
#
hip hip hurray! hip hip hurray! hip hip hurray!
- 可以用 in 和 not in 判断成员是否在字符串内:
sub_string1 = 'ice'
sub_string2 = 'glue'
string1 = 'ice cream'
if sub_string in string1:
print("There is " + sub_string + " in " + string1)
if sub_string2 not in string1:
print("Phew! No " + sub_string2 + " in " + string1)
#
#
There is ice in ice cream
Phew! No glue in ice cream
- 利用 str.capitalize()实现首字母大写:
str.capitalize('cookie')
#
#
'Cookie'
- 利用str.islower()判断字符串是否是小写:
snack = 'cookie'
snack.islower()
#
#
True
- 利用str.find(substring)返回找到子串的字符串中开头的索引:
str1 = 'I got you a cookie'
str2 = 'cook'
str1.find(str2)
#
#
12
- 利用str.count(substring)计算字符串中出现子字符串的次数, 您也可以指定字符串的开始和结束索引:
str1 = 'I got you a cookie, do you like cookies?'
str2 = 'cookie'
str1.count(str2)
#
#
2
- 利用 str.isspace()判断字符串中是否有空格:
str_space = ' '
str_space.isspace()
#
#
True
str_tab = '\t'
str_tab.isspace()
#
#
True
str_nextline = '''\n'''
str_nextline.isspace()
#
#
True
- 利用str.lstrip() 删除字符串中的所有开头空格。:
str1 = " I can't hear you. Are you alright? "
str2 = " Yes, all is good."
str3 = str1.lstrip() + str2.lstrip()
print(str3)
#
#
I can't hear you. Are you alright? Yes, all is good.
- 利用 str.isdigit()判断字符串是不是数字:
number_string = "1020304050"
number_string.isdigit()
#
#
True
- 利用str.replace(substring, new) 进行替换:
string1 = 'hip hip hurray! hip hip hurray! hip hip hurray!'
string2 = string1.replace('hip', 'Hip')
print(string1)
print(string2)
#
#
hip hip hurray! hip hip hurray! hip hip hurray!
Hip Hip hurray! Hip Hip hurray! Hip Hip hurray!
#定义max来指定替换最大个数
string1.replace('hip', 'Hip', 2)
#
#
'Hip Hip hurray! hip hip hurray! hip hip hurray!'
- str.split(delimiter=”“) 指定符号进行字符串分割:
str.split(delimiter="")
#
#
list_dessert = string1.split(',')
字符串格式化 |
print("I bought %d Euro worth of %s!" %(200, 'cookies'))
#
#
I bought 200 Euro worth of cookies!
print("I bought {0} Euro worth of {1}!".format(200,'cookies')) #Accessing values by position
#
#
I bought 200 Euro worth of cookies!
print("I bought {total} Euro worth of {item}!".format(total = 200, item = 'cookies')) #Accessing values by name
#
#
I bought 200 Euro worth of cookies!
'{:#<10}'.format('Cake') #Left aligment for word 'Cake' according to right alignment, gaps filled with '#'
#
#
'Cake######'
'{:#^10}'.format('Cake') #Centre aligment for word 'Cake' according to right alignment, gaps filled with '#'
#
#
'###Cake###'
'{:#>10}'.format('Cake') #Right aligment for word 'Cake' according to right alignment, gaps filled with '#'
#
#
'######Cake'
for num in range(1,10):
print('{0:{width}}'.format(num, width=5), end=' ')
#
#
1 2 3 4 5 6 7 8 9
- Template模块
from string import Template #First you will need to import 'Tempalte' class
money = dict(who = 'You', to_whom = 'baker')
Template('$who owe the $to_whom a total of $$100').substitute(money)
#
#
'You owe the baker a total of $100'
word = dict(noun = 'feed')
Template('Please don\'t stop ${noun}ing me').substitute(word)
#
#
"Please don't stop feeding me"
fact = Template('$alter_ego is weak but wait till he transforms to $superhero!')
fact.substitute(alter_ego='Bruce Banner', superhero='Hulk')
#
#
'Bruce Banner is weak but wait till he transforms to Hulk!'
hero = dict(alter_ego='Peter Parker')
fact.substitute(hero)
#
#
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-244-c82f6a2ebc02> in <module>()
1 hero = dict(alter_ego='Peter Parker')
----> 2 fact.substitute(hero)
~/anaconda3/envs/tensorflow/lib/python3.5/string.py in substitute(*args, **kws)
127 raise ValueError('Unrecognized named group in pattern',
128 self.pattern)
--> 129 return self.pattern.sub(convert, self.template)
130
131 def safe_substitute(*args, **kws):
~/anaconda3/envs/tensorflow/lib/python3.5/string.py in convert(mo)
117 named = mo.group('named') or mo.group('braced')
118 if named is not None:
--> 119 val = mapping[named]
120 # We use this idiom instead of str() because the latter will
121 # fail if val is a Unicode containing non-ASCII characters.
KeyError: 'superhero'
- 利用 :
fact.safe_substitute(hero)
#
#
'Peter Parker is weak but wait till he transforms to $superhero!'
print(number_string[::-2])
#
#
00000
常用字符串操作 |
#连接
string1 = 'Chocolate'
string2 = 'cookie'
snack = string1 + " " + string2
print(snack)
#
#
Chocolate cookie
然而,字符串与数字进行连接时,数字为数值型是会报错的:
cost = 15
string1 = "The total in Euro is: "
bill = string1 + cost
print(bill)
#
#
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-218-7d5c5248b927> in <module>()
2 string1 = "The total in Euro is: "
3
----> 4 bill = string1 + cost
5 print(bill)
TypeError: Can't convert 'int' object to str implicitly
如下操作就可以:
bill = string1 + str(cost)
print(bill)
#
#
The total in Euro is: 15
- 复制字符串可以用 * 操作:
single_word = 'hip '
line1 = single_word * 2 + 'hurray! '
print(line1 * 3)
#
#
hip hip hurray! hip hip hurray! hip hip hurray!
- 可以用 in 和 not in 判断成员是否在字符串内:
sub_string1 = 'ice'
sub_string2 = 'glue'
string1 = 'ice cream'
if sub_string in string1:
print("There is " + sub_string + " in " + string1)
if sub_string2 not in string1:
print("Phew! No " + sub_string2 + " in " + string1)
#
#
There is ice in ice cream
Phew! No glue in ice cream
- 利用 str.capitalize()实现首字母大写:
str.capitalize('cookie')
#
#
'Cookie'
- 利用str.islower()判断字符串是否是小写:
snack = 'cookie'
snack.islower()
#
#
True
- 利用str.find(substring)返回找到子串的字符串中开头的索引:
str1 = 'I got you a cookie'
str2 = 'cook'
str1.find(str2)
#
#
12
- 利用str.count(substring)计算字符串中出现子字符串的次数, 您也可以指定字符串的开始和结束索引:
str1 = 'I got you a cookie, do you like cookies?'
str2 = 'cookie'
str1.count(str2)
#
#
2
- 利用 str.isspace()判断字符串中是否有空格:
str_space = ' '
str_space.isspace()
#
#
True
str_tab = '\t'
str_tab.isspace()
#
#
True
str_nextline = '''\n'''
str_nextline.isspace()
#
#
True
- 利用str.lstrip() 删除字符串中的所有开头空格:
str1 = " I can't hear you. Are you alright? "
str2 = " Yes, all is good."
str3 = str1.lstrip() + str2.lstrip()
print(str3)
#
#
I can't hear you. Are you alright? Yes, all is good.
- 利用 str.isdigit()判断字符串是不是数字:
number_string = "1020304050"
number_string.isdigit()
#
#
True
- 利用str.replace(substring, new) 进行替换:
string1 = 'hip hip hurray! hip hip hurray! hip hip hurray!'
string2 = string1.replace('hip', 'Hip')
print(string1)
print(string2)
#
#
hip hip hurray! hip hip hurray! hip hip hurray!
Hip Hip hurray! Hip Hip hurray! Hip Hip hurray!
#定义max来指定替换最大个数
string1.replace('hip', 'Hip', 2)
#
#
'Hip Hip hurray! hip hip hurray! hip hip hurray!'
- str.split(delimiter=”“) 指定符号进行字符串分割:
str.split(delimiter="")
#
#
list_dessert = string1.split(',')
字符串格式化 |
print("I bought %d Euro worth of %s!" %(200, 'cookies'))
#
#
I bought 200 Euro worth of cookies!
print("I bought {0} Euro worth of {1}!".format(200,'cookies')) #Accessing values by position
#
#
I bought 200 Euro worth of cookies!
print("I bought {total} Euro worth of {item}!".format(total = 200, item = 'cookies')) #Accessing values by name
#
#
I bought 200 Euro worth of cookies!
'{:#<10}'.format('Cake') #Left aligment for word 'Cake' according to right alignment, gaps filled with '#'
#
#
'Cake######'
'{:#^10}'.format('Cake') #Centre aligment for word 'Cake' according to right alignment, gaps filled with '#'
#
#
'###Cake###'
'{:#>10}'.format('Cake') #Right aligment for word 'Cake' according to right alignment, gaps filled with '#'
#
#
'######Cake'
for num in range(1,10):
print('{0:{width}}'.format(num, width=5), end=' ')
#
#
1 2 3 4 5 6 7 8 9
- Template模块
from string import Template #First you will need to import 'Tempalte' class
money = dict(who = 'You', to_whom = 'baker')
Template('$who owe the $to_whom a total of $$100').substitute(money)
#
#
'You owe the baker a total of $100'
word = dict(noun = 'feed')
Template('Please don\'t stop ${noun}ing me').substitute(word)
#
#
"Please don't stop feeding me"
fact = Template('$alter_ego is weak but wait till he transforms to $superhero!')
fact.substitute(alter_ego='Bruce Banner', superhero='Hulk')
#
#
'Bruce Banner is weak but wait till he transforms to Hulk!'
hero = dict(alter_ego='Peter Parker')
fact.substitute(hero)
#
#
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-244-c82f6a2ebc02> in <module>()
1 hero = dict(alter_ego='Peter Parker')
----> 2 fact.substitute(hero)
~/anaconda3/envs/tensorflow/lib/python3.5/string.py in substitute(*args, **kws)
127 raise ValueError('Unrecognized named group in pattern',
128 self.pattern)
--> 129 return self.pattern.sub(convert, self.template)
130
131 def safe_substitute(*args, **kws):
~/anaconda3/envs/tensorflow/lib/python3.5/string.py in convert(mo)
117 named = mo.group('named') or mo.group('braced')
118 if named is not None:
--> 119 val = mapping[named]
120 # We use this idiom instead of str() because the latter will
121 # fail if val is a Unicode containing non-ASCII characters.
KeyError: 'superhero'
fact.safe_substitute(hero)
#
#
'Peter Parker is weak but wait till he transforms to $superhero!'