python字符串操作(连接、比较、格式化等)

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} h3 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:3; font-size:13.5pt; font-family:"Times New Roman";} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} pre {margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Courier New"; mso-fareast-font-family:宋体;} span.string {mso-style-name:string;} span.keyword {mso-style-name:keyword;} span.comment {mso-style-name:comment;} span.number {mso-style-name:number;} span.special {mso-style-name:special;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:72165447; mso-list-template-ids:-1133845224;} @list l1 {mso-list-id:602418790; mso-list-template-ids:1577338694;} @list l2 {mso-list-id:674960509; mso-list-template-ids:-1930949950;} @list l3 {mso-list-id:705984727; mso-list-template-ids:1577643100;} @list l4 {mso-list-id:1042285861; mso-list-template-ids:-102869004;} @list l5 {mso-list-id:1450004386; mso-list-template-ids:1691506488;} @list l6 {mso-list-id:1739205695; mso-list-template-ids:1144555762;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} -->

python字符串操作(连接、比较、格式化等)

http://canlynet.javaeye.com/blog/675250

 

 

字符串连接
方法一:

Python代码

  1. >>> str1 = 'hello'   
  2. >>> str2 = 'world'   
  3. >>> str1_2 = str1 + ' '  + str2  
  4. >>> str1_2  
  5. 'hello world'   
  6. >>> print  str1_2  
  7. hello world  
  8. >>>  
>>> str1 = 'hello'
>>> str2 = 'world'
>>> str1_2 = str1 + ' ' + str2
>>> str1_2
'hello world'
>>> print str1_2
hello world
>>> 



方法二:

Python代码

  1. >>> str12 = '%s %s'  % (str1, str2)  
  2. >>> print  str12  
  3. hello world  
>>> str12 = '%s %s' % (str1, str2)
>>> print str12
hello world



注意,下面方式是利用 str1 str2初始化一个元组:

Python代码

  1. >>> str_12 = str1, str2  
  2. >>> str_12  
  3. ('hello''world' )  
  4. # 方括弧才是列表,元组是不可改变滴   
  5. >>> str_1_2 = ['hello''world' ]  
  6. >>> str_1_2  
  7. ['hello''world' ]  
  8. # 另外顺便提一下, print 末尾加逗号是把换行符替代成一个空格的意思。   
  9. >>> print  'hello' ,/  
  10. ... 'world'    
  11. hello world  
>>> str_12 = str1, str2
>>> str_12
('hello', 'world')
#
方括弧才是列表,元组是不可改变滴

>>> str_1_2 = ['hello', 'world']
>>> str_1_2
['hello', 'world']
#
另外顺便提一下,
print
末尾加逗号是把换行符替代成一个空格的意思。

>>> print 'hello',/
... 'world' 
hello world



===============================================

字符串比较

Python代码

  1. >>> str = 'info'  # 我就犯这个错误,因为 c 中用 str 做变量很简洁   
  2. >>> cmp(str, 'info' )  
  3. 0   
  4.   
  5. >>> str == 'info'  # 返回值类型不同!!!尽管 if 语句中都能使用 ...   
  6. True   
>>> str = 'info' #
我就犯这个错误,因为
c
中用
str
做变量很简洁

>>> cmp(str, 'info')
0
 
>>> str == 'info' #
返回值类型不同!!!尽管
if
语句中都能使用
...
True



=================================================

字符串注意事项:
1. 不要像 C语言那样使用 str作为变量,因为 str python中是一个关键字,用于转换成字符串类型。

Python代码

  1. >>> str = 'hello'   
  2. >>> i = 5   
  3. >>> str1 = str(i)  
  4. Traceback (most recent call last):  
  5.   File "<stdin>" , line 1in  <module>  
  6. TypeError: 'str'  object is  not  callable  
>>> str = 'hello'
>>> i = 5
>>> str1 = str(i)
Traceback (most recent call last):
  
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable


退出 python后重新进入(只能退出重新运行,因为这里不是命令行而是 python在对 stdin进行解释执行(跟读入一个文件执行是一个道理,不过每次都要等待输入一行而已,行缓冲):

Python代码

  1. >>> str1 = 'hello'   
  2. >>> i = 5   
  3. >>> str2 = str(i)  
  4. >>> print  str2  
  5. 5   
>>> str1 = 'hello'
>>> i = 5
>>> str2 = str(i)
>>> print str2
5



2. 要理解 for x in yyy: x不是关键词,而是一个我们定义的变量, python每次循环为这个变量赋值 yyy中的 1个(从最小到最大序号)。
如:

Python代码

  1. >>> str1 = ['str1''str2''str3' ]  
  2. >>> for  each in  str1: # 这里的 each 可以是任何变量名   
  3. ...     print  each,  
  4. ...  
  5. str1 str2 str3  
  6. >>>  
 
>>> str1 = ['str1', 'str2', 'str3']
>>> for each in str1: #
这里的
each
可以是任何变量名

...     
print each,
...
str1 str2 str3
>>> 
 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值