<!-- /* 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代码
- >>> str1 = 'hello'
- >>> str2 = 'world'
- >>> str1_2 = str1 + ' ' + str2
- >>> str1_2
- 'hello world'
- >>> print str1_2
- hello world
- >>>
方法二:
Python代码
- >>> str12 = '%s %s' % (str1, str2)
- >>> print str12
- hello world
注意,下面方式是利用 str1, str2初始化一个元组:
Python代码
- >>> 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代码
- >>> str = 'info' # 我就犯这个错误,因为 c 中用 str 做变量很简洁
- >>> cmp(str, 'info' )
- 0
- >>> str == 'info' # 返回值类型不同!!!尽管 if 语句中都能使用 ...
- True
=================================================
字符串注意事项:
1. 不要像 C语言那样使用 str作为变量,因为 str在 python中是一个关键字,用于转换成字符串类型。
Python代码
- >>> 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代码
- >>> str1 = 'hello'
- >>> i = 5
- >>> str2 = str(i)
- >>> print str2
- 5
2. 要理解 for x in yyy:中 x不是关键词,而是一个我们定义的变量, python每次循环为这个变量赋值 yyy中的 1个(从最小到最大序号)。
如:
Python代码
- >>> str1 = ['str1' , 'str2' , 'str3' ]
- >>> for each in str1: # 这里的 each 可以是任何变量名
- ... print each,
- ...
- str1 str2 str3
- >>>