>>> temp = 42
>>> print "The temperature is " + temp
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print "The temperature is " + temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print "The temperature is " + `temp`
The temperature is 42
4、input和raw_input对比
input 会【假设】用户输入的事合法的Python表达式。
而raw_input会把所有的输入当做原始数据(raw data),然后将其放入字符串中:
>>> name = input("What is your name?")
What is your name?jason
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
name = input("What is your name?")
File "<string>", line 1, in <module>
NameError: name 'jason' is not defined
>>> input("Enter a number:")
Enter a number:3
3
>>> raw_input("Enter a number:")
Enter a number:3
'3'
5、长字符串、原始字符串和Unicode
》长字符串:
’‘’ ** ‘’‘ 可以跨行,不需要转义其中引号
》原始字符串:
对于路径的情况,往往需要将所有的\换成\\,这样需要复制之后做很多添加,原始字符串可以解决这个问题