1.中文问题
如果你的python文件需要使用中文,需要在文件的开始使用#coding=utf-8
## 2 .The difference of input and raw_input##
name = input("input your name:")
print "your name is " + name
上述代码中,执行第一条语句的时候,是没有错的,但是执行到第二条语句的时候就会出现错误。原因是:input会假设用户输入的是合法的python表达式。
当然你可以使用 str ,repr, “这三种方式来对name进行字符串转义。你也可以在输入的时候,“like this”.不过这对于用户来说,太过分了不是吗
name = raw_input("input your name:")
print "your name is " + name
上述代码在执行的时候是正确的,因为raw_input 会把所有的输入当做原始数据。hetland告诉我们尽可能的使用raw_input函数。
## 字符串 ##
python中使用双引号和单引号都是ok的,但是引用单双引号也是有意义的。
print "let's go"
print '"hello world" i said'
上述的语句都是没有问题的。编译器能够很好的处理上述字符串
print 'let's go'
print ""hello world " she said"
如果我们按照上述的方式来写的话,python编译器依然懵逼,不知所措了。
当然我们也可以使用转移字符来进行
print 'let\' go'
print "\"hello world\" she said"
长字符串:
print ''' this is a long string ,
it continues here,
and it's not over yet,
not yet,
you can say "hello world"
here
'''
长字符串是使用3个单引号或者3个双引号来使用,中间可以随意的换行,以及使用单双引号。
如果使用普通字符串也可以换行,但是要使用换行符
print 'this is a long \
string'
原始字符串:
有时候我们希望表达这样的字符串path = ‘c:\nowhere’如果不进行转义,打印会变成:
》》》path
c:
where
这显然不是我们需要的。这里我们可以使用转义来完成:
path = 'c:\\nowhere'
或者使用原始字符串
path = r'c:\nowhere'
都可以正常表示。