字符串连接
方法一:
>>> str1 = 'hello'
>>> str2 = 'world'
>>> str1_2 = str1 + ' ' + str2
>>> str1_2
'hello world'
>>> print str1_2
hello world
>>>
方法二:
>>> str12 = '%s %s' % (str1, str2)
>>> print str12
hello world
注意,下面方式是利用str1,str2初始化一个元组:
>>> str_12 = str1, str2
>>> str_12
('hello', 'world')
#方括弧才是列表,元组是不可改变滴
>>> str_1_2 = ['hello', 'world']
>>> str_1_2
['hello', 'world']
#另外顺便提一下,print末尾加逗号是把换行符替代成一个空格的意思。
>>> print 'hello',\
... 'world'
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
===============================================
字符串比较
>>> 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进行解释执行(跟读入一个文件执行是一个道理,不过每次都要等待输入一行而已,行缓冲):