1、获取用户输入input:python3以上的版本取消了input()与raw_input()的区别,统一用input()代替,返回值为str类型,而进行类型强制转换:x=int(input())
>>> x =int( input("x:")) x:34 >>> y =int( input("y:")) y:42 >>> x*y 1428 >>>
2、脚本运行,避免程序运行完就关闭:在程序最后加一行代码:input ("press<enter>")
name = input ("what is your name?") print ("hello,"+name+"!") input ("press <enter>")
3、str和repr函数
str:将值转换为合理形式的字符串
repr:创建一个字符串,以合法的python表达式的形式表示值
>>> print (repr("hello,world!")) 'hello,world!' >>> print (str("hello,world!")) hello,world! >>>
4、长字符串、原始字符串和Unicode
长字符串
需要跨多行,用三个引号,单引号/双引号都可以
print ('''this is a very long string. it continunes here. and it's not over yet. "hello,wprld!" still here.''')
运行结果:

*普通字符串跨行:使用反斜杠 \,一行中最后一个字符是反斜杠,那么换行符本身就“转义”了,即被忽略了
eg.
print ("hello,\ world")
打印结果为:hello,world
同适用于表达式和语句
>>> 1+2+\ 4+5 12 >>> print \ ("hello,world!") hello,world! >>>
原始字符串:使用r作为前缀
普通字符中,反斜线有特殊作用:转义。例如,换行符可以写为\n,并用于字符串中
eg.
>>> print ("hello,\nworld!") hello, world!
如果打印一个路径,例如
>>>print ('C:\nowhere') #打印结果如下 C: owhere #通过反斜线进行转义,改为: >>>print ('C:\\nowhere')
C:\nowhere
>>>
长路径:原始字符串起作用啦
>>>print (r'C:\nowhere') C:\nowhere >>>print (r'C:\Program Files\fnord\foo\bar\baz\frozz\bozz') C:\Program Files\fnord\foo\bar\baz\frozz\bozz
*原始字符最后一个字符不能是反斜线
浙公网安备 33010602011771号