X Tutup
#字符串(2) ##键盘输入 电脑的智能,一种体现就是可以接受用户通过键盘输入的内容。 通过Python能不能实现这个简单的功能呢?当然能,要不然Python如何横行天下呀。 不过在写这个功能前,要了解函数: - Python 2:`raw_input()` - Python 3: `input()` 这是Python的内建函数(built-in function)。关于内建函数,可以分别通过下面的链接查看: - [Python 2的内建函数](https://docs.python.org/2/library/functions.html) - [Python 3的内建函数](https://docs.python.org/3.5/library/functions.html) 如果仔细对照上面的两个版本的内建函数,会发现还是有差异的。 这些内建函数,怎么才能知道哪个函数怎么用,是干什么用的呢? 一种方法是通过网页上的官方内容,点击链接,就能查看该函数的说明文档。 还有一种方法,不知道你是否还记得我在前面使用过的,这里再进行演示。 >>> help(raw_input) #Python 2 >>> help(input) #Python 3 然后就出现, Python 2中的结果: Help on built-in function raw_input in module __builtin__: raw_input(...) raw_input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading. Python 3中的结果: Help on built-in function input in module builtins: input(prompt=None, /) Read a string from standard input. The trailing newline is stripped. The prompt string, if given, is printed to standard output without a trailing newline before reading input. If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. On *nix systems, readline is used if available. 从中是不是已经清晰地看到了`raw_input()`或者`input()`的使用方法了。 下面就在交互模式下操练一下这个主管键盘输入的函数。 分别在交互模式下,将这个两个函数操练一下。 >>> raw_input("input your name:") # Python 2 input your name:python #提示输入内容,通过键盘输入`python` 'python' >>> input("input your name:") #Python 3 input your name:python #提示输入内容,通过键盘输入`python` 'python' 输入名字之后,就返回了输入的内容。 返回的结果,也是一个对象(字符串类型的对象),那么就可以用赋值语句,与一个变量关联起来。 >>> name = raw_input("input your name:") #Python 2 input your name:python >>> name 'python' >>> type(name) >>> name = input("input your name:") #Python 3 input your name:python >>> name 'python' >>> type(name) 而且,返回的结果是str类型。如果输入的是数字呢? >>> age = raw_input("How old are you?") #Python 2 How old are you?10 >>> age '10' >>> type(age) >>> age = input("How old are you?") #Python 2 How old are you?10 >>> age '10' >>> type(age) 返回的结果,仍然是str类型。 所以,Python 2的文档中就明确写出`raw_input([prompt]) -> string`,意思是它的返回值为字符串。 `print()`在Python 2和Python 3中,都是一个函数。 特别要提醒的是,`print()`默认是以`\n`结尾的,所以,每次用到`print`或者`print()`之后,输出内容后面自动带上了`\n`,于是在打印的结果中就换行了。 有了以上两个准备,接下来就可以写一个能够“对话”的小程序了。 #!/usr/bin/env python # coding=utf-8 name = raw_input("What is your name?") #如果是在Python 3中,更换为input() age = raw_input("How old are you?") print "Your name is: ", name #Python 3: print("Your name is: ", name) print "You are " + age + " years old." #Python 3: print("You are " + age + " years old.") after_ten = int(age) + 10 print "You will be " + str(after_ten) + " years old after ten years." #Python 3: print("You will be " + str(after_ten) + " years old after ten years.") 读者是否能独立调试这个程序? `print`语句或者`print()`函数,除了打印一个字符串之外,还可以打印字符串拼接结果(拼接之后还是一个字符串,就是比原来长了)。 print "You are " + age + " years old." #Python 2 print("You are " + age + " years old.") #Python 3 注意,那个变量`age`必须指向的是字符串类型的对象,如最后的那个语句中: print "You will be " + str(after_ten) + " years old after ten years." #Python 2 print("You will be " + str(after_ten) + " years old after ten years.") #Python 3 这句话里面,有一个类型转化,将原本是整数型的对象转化为了str类型。否则,就报错,不信,你可以试试。 同样注意,在`after_ten = int(age) + 10`中,因为通过`raw_input()`或者`input()`得到的是str类型,当age和10求和的时候,需要先用`int()`函数进行类型转化,才能和后面的整数10相加。 这个小程序,是有点综合的,基本上把已经学到的东西综合运用了一次。请仔细调试一下,如果没有通过,看报错信息,你能够从中获得修改方向的信息。 通过键盘输入得到的都是字符串,也有的字符串不是通过键盘输入得到的,需要用引号包裹,有时候还要用转义符。但是,有一种方式,能够还原字符串中字符的原始含义。 ##原始字符串 所谓原始字符串,就是指字符串里面的每个字符都是原始含义,比如反斜杠,不会被看做转义符。 在一般字符串中,比如 >>> print "I like \npython" #Python 3: print("I like \npython") I like python 这里的反斜杠就不是“反斜杠”的原始符号含义,而是和后面的n一起组成了换行符`\n`,即转义了。当然,这似乎没有什么太大影响,但有的时候,可能会出现问题,比如打印DOS路径(DOS,有没有搞错,现在还有人用吗?) >>> dos = "c:\news" >>> dos 'c:\news' #这里貌似没有什么问题 >>> print dos #当用print来打印这个字符串的时候,就出问题了。 c: ews #Python 3: print(dos) 如何避免? 用转义符可以解决: >>> dos = "c:\\news" >>> print dos #Python 3: print(dos) c:\news 此外,还有一种方法,如: >>> dos = r"c:\news" >>> print dos #Python 3: print(dos) c:\news >>> print r"c:\news\python" #Python 3: print(r"c:\news\python") c:\news\python 状如`r"c:\news"`,由r开头引起的字符串,就是原始字符串,在里面放任何字符都表示该字符的原始含义。 这种方法在做网站设置网站目录结构的时候非常有用。使用了原始字符串,就不需要转义了。 一个字符串,一般可以有多个字符构成,那么可以操作每个字符吗?这就要索引和切片。 ------ [总目录](./index.md)   |   [上节:字符串(1)](./106.md)   |   [下节:字符串(3)](./108.md) 如果你认为有必要打赏我,请通过支付宝:**qiwsir@126.com**,不胜感激。
X Tutup