转 python基础学习笔记(一)
http://www.cnblogs.com/fnng/category/454439.html
1 下面我们创建一个文件 2 root@fnngj-H24X:/hzh/python# touch hell.py 3 root@fnngj-H24X:/hzh/python# ls 4 hell.py 5 root@fnngj-H24X:/hzh/python# vi hell.py 6 输入: 7 print “hello,world!” 8 9 保存退出。运行 10 root@fnngj-H24X:/hzh/python# python hell.py 11 hello,world!
input()函数会假设用户输入的是合法的python表达式。所以直接输入huhu 系统会提示错误,但是如果加上引号(“huhu”)就会是一个合法的字符,程序运行是没有问题的。
然而,要求用户带着引号输入他们的名字有点过份,因此,就这需要使用raw_input函数。
>>> input("enter a namber:") enter a namber:3 >>> raw_input("enter a namber:") enter a namber:3 '3'
当然input有特别的需要,比如要求用户输入数字时。
长字符串
如果需要写一个非常非常长的字符串,它需要跨多行,那么,可以使用三个引号带点普通引号。
>>> print ''' this is a very long string. It continues here. and it's not over yet. ''' this is a very long string. It continues here. and it's not over yet.
普通字符串也可以跨行。如果一行之中最后一个字符是反斜线,那么,换行符本身就“转义”了,也就是被忽略了。
>>> print " Hello.\ world!" Hello.world! >>> 1+2+\ 4+5
原始字符串
>>> print r'C:\Program Files\fnord\foo\bar\baz\frozz' C:\Program Files\fnord\foo\bar\baz\frozz >>> print r'Let\'s go !' Let\'s go !
原始字符串不会把反斜线当作特殊字符。可以看到,原始字符串以r开头。
浙公网安备 33010602011771号