一 string与引号
Python中的字符串可以使用四种引号来表示,‘(单引号),“(双引号),’‘'(三单引号),"""(三双引号)。 且他们均有相同的意思,需成对出现,单双引号可以相互嵌套。没有任何区别。
>>>
print
(
'
he said "good", you said "great", and i want to say """excellent"""
'
)
he said " good " , you said " great " , and i want to say """ excellent """
he said " good " , you said " great " , and i want to say """ excellent """
二 string的join和split函数
#
string compare
cmpStr = " abc "
if cmpStr == " abc " :
print ( " abc " + " Matches " + cmpStr)
if cmpStr.upper() == ' abc ' .upper():
print (cmpStr + ' Matches ' + ' ABC ' + ' whit ignoring case ' )
# abc Matches abc
# abc Matches ABC whit ignoring case
# string join
wordList = [ " A " , " few " , " more " , " good " , " words " ]
print ( " List: " + ' ' .join(wordList))
# List: A few more good words
# string split
sentence = " A Simple Sentence. "
print (sentence.split())
# ['A', 'Simple', 'Sentence.']
cmpStr = " abc "
if cmpStr == " abc " :
print ( " abc " + " Matches " + cmpStr)
if cmpStr.upper() == ' abc ' .upper():
print (cmpStr + ' Matches ' + ' ABC ' + ' whit ignoring case ' )
# abc Matches abc
# abc Matches ABC whit ignoring case
# string join
wordList = [ " A " , " few " , " more " , " good " , " words " ]
print ( " List: " + ' ' .join(wordList))
# List: A few more good words
# string split
sentence = " A Simple Sentence. "
print (sentence.split())
# ['A', 'Simple', 'Sentence.']
三 字符串的子串查找和替换
#
string find
searchStr = " Red Blue Violet Green Blue Yellow Black "
print (searchStr.find( " Red " ))
print (searchStr.rfind( " Blue " ))
print (searchStr.index( " Blue " ))
print (searchStr.index( " Blue " , 20 ))
print (searchStr.rindex( " Blue " ))
print (searchStr.rindex( " Blue " , 1 , 18 ))
f = ' file.py '
if f.endswith( ' .py ' ):
print ( " Python file: " + f)
elif f.endswith( ' .txt ' ):
print ( " Text file: " + f)
# string replace
question = " What is the air speed velocity of an unlaiden swallow? "
question2 = question.replace( " swallow " , " European swallow " )
print (question2)
searchStr = " Red Blue Violet Green Blue Yellow Black "
print (searchStr.find( " Red " ))
print (searchStr.rfind( " Blue " ))
print (searchStr.index( " Blue " ))
print (searchStr.index( " Blue " , 20 ))
print (searchStr.rindex( " Blue " ))
print (searchStr.rindex( " Blue " , 1 , 18 ))
f = ' file.py '
if f.endswith( ' .py ' ):
print ( " Python file: " + f)
elif f.endswith( ' .txt ' ):
print ( " Text file: " + f)
# string replace
question = " What is the air speed velocity of an unlaiden swallow? "
question2 = question.replace( " swallow " , " European swallow " )
print (question2)
对于子字符串的查找,可以使用in,可读性更好。
if
'
hello world,hello
'
.find(
'
world
'
)
!=
-
1
:
print
(
'
find
'
)
if ' world ' in ' hello world,hello ' : print ( ' find ' )
if ' world ' in ' hello world,hello ' : print ( ' find ' )
四 print函数中str的格式
#
string rjust and ljust
chapters = { 1 : 5 , 2 : 46 , 3 : 52 , 4 : 87 , 5 : 90 }
for x in chapters:
print ( " Chapter " + str(x) + str(chapters[x]).rjust( 15 , ' . ' ))
# Chapter 1..............5
# Chapter 2.............46
# Chapter 3.............52
# Chapter 4.............87
# Chapter 5.............90
# print
name = ' buddy '
print ( " welcome " + " " + name + " , you are very handsome! " )
print ( " welcome " , name, " , you are very handsome! " )
print ( ' welcome %s, you are very handsome! ' % name)
# welcome buddy, you are very handsome!
# welcome buddy , you are very handsome!
# welcome buddy, you are very handsome!
# print string format
chapters2 = { 1 : 5 , 2 : 46 , 3 : 52 , 4 : 87 , 5 : 90 }
for x in chapters2:
print ( " Chapter %d %15s " % (x,str(chapters2[x])))
# Chapter 1 5
# Chapter 2 46
# Chapter 3 52
# Chapter 4 87
# Chapter 5 90
chapters = { 1 : 5 , 2 : 46 , 3 : 52 , 4 : 87 , 5 : 90 }
for x in chapters:
print ( " Chapter " + str(x) + str(chapters[x]).rjust( 15 , ' . ' ))
# Chapter 1..............5
# Chapter 2.............46
# Chapter 3.............52
# Chapter 4.............87
# Chapter 5.............90
name = ' buddy '
print ( " welcome " + " " + name + " , you are very handsome! " )
print ( " welcome " , name, " , you are very handsome! " )
print ( ' welcome %s, you are very handsome! ' % name)
# welcome buddy, you are very handsome!
# welcome buddy , you are very handsome!
# welcome buddy, you are very handsome!
# print string format
chapters2 = { 1 : 5 , 2 : 46 , 3 : 52 , 4 : 87 , 5 : 90 }
for x in chapters2:
print ( " Chapter %d %15s " % (x,str(chapters2[x])))
# Chapter 1 5
# Chapter 2 46
# Chapter 3 52
# Chapter 4 87
# Chapter 5 90
print函数中使用%来隔离格式str和变量。
五 使用str.format来格式字符串
p4newuser
=
'
BBB
'
p4newuseremail = ' BBB@example.com '
p4newuserfullname = ' BBB Frist '
userspec = str.format( " \
User: %s \n\
Email: %s \n\
Update: \n\
Access: \n\
FullName: %s \n\
Password: \n\
" % (p4newuser,p4newuseremail,p4newuserfullname))
print userspec
p4newuseremail = ' BBB@example.com '
p4newuserfullname = ' BBB Frist '
userspec = str.format( " \
User: %s \n\
Email: %s \n\
Update: \n\
Access: \n\
FullName: %s \n\
Password: \n\
" % (p4newuser,p4newuseremail,p4newuserfullname))
print userspec
结果:
User: BBB
Email: BBB@example.com
Update:
Access:
FullName: BBB Frist
Password:
完!