python语法31[string的print和format]

 

一 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 """
 

二 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.']


三 字符串的子串查找和替换

# 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)

 

对于子字符串的查找,可以使用in,可读性更好。

if   ' hello world,hello ' .find( ' world ' !=   - 1  :  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

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

结果:

User: BBB
Email: BBB@example.com
Update:
Access:
FullName: BBB Frist
Password:

 

 完!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值