Python 第一章 基础知识(9) 字符串

来源:互联网 发布:matlab c语言 vb 编辑:程序博客网 时间:2024/06/10 11:13

字符串


引号字符串和转义引号

双引号字符串,单引号字符串

>>>"Hello, world!"

'Hello, world!'

>>>‘Hello, World!’

'Hello, World!'

这里能看见双引号和单引号的可以。但是在某些情况下,单双引号得一起才合理。


>>> 'let's go'
SyntaxError: invalid syntax
>>>
当字符串中有单引号的时候,用单引号将字符串括起来就会产生混淆。因为Python并不知道如何处理s之后的字符。

那么这种情况可以用双引号将字符串括起来。

>>> "let's go"
‘let's go’


转义引号

上面的例子也可用反斜线(\)对字符串中的引号进行转义。

>>> 'let\'s go'
"let's go"

用反斜线标记一下,Python会将中间的单引号理解为字符串中的一个字符,而不是字符换的结束。

>>> "\"Hello, World!\" she said"
'"Hello, World!" she said'


反斜线的这种用法比较麻烦,以后还会介绍通过使用长字符串和原始字符串来减少绝大多数反斜线的使用。



拼接字符串

用一个字符串接着另外一个字符串的方式写两个字符串,Python就会自动拼接他们。

>>> "Let's say " '"Hello, world"'
'Let\'s say "Hello, world"'

但这种方式并不常用,下面来介绍拼接字符串的一般方法

就像运行加法运算一下,将赋予了字符串值的x和y相加。

>>> x = "Hello."
>>> y = "World!"
>>> x + y
'Hello.World!'
>>>


字符串表示,str和repr

str函数, 会把值转换为合理形式的字符串,以便用户可以理解

repr函数,会创建一个字符串,以合法的Python表达式的形式表示值

>>> print "Hello"
Hello
>>> print str("Hello")
Hello
>>> print repr("Hello")
'Hello'
>>>

在举例子说明repr(x)的功能。如果打印一个包含数字的句子。

>>> temp = 42
>>> print 'The temperature is ' + temp
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    print 'The temperature is ' + temp
TypeError: cannot concatenate 'str' and 'int' objects
>>> print 'The temperature is ' + repr(temp)
The temperature is 42

>>> print "The temperature is " + str(temp)
The temperature is 42

运行print 'The temperature is ' + temp 出错时因为不可以直接用字符+数字。而用repr(temp),创建了一个于42数值表现一样的字符串“42”。


str, repr是将Python值转换为字符串的两种方法。两者的区别就是str让字符串更易阅读(打印出来的字符串无引号),而repr则字符串转换为合法的Python表达式(有引号)。



长字符串、原始字符串和Unicode

长字符串

如果需要写一个非常非常长的字符串,需要跨多行,那么,可以用三个单(双)引号代替普通的引号

>>> print '''This is very long string.
it contiues here.
and it's not over yet.
end here.'''
This is very long string.
it contiues here.
and it's not over yet.
end here.
>>>

因为三个引号得特殊引用凡是,可以再字符串中同时使用单引号和双引号,而不需要使用反斜线进行转义。


原始字符串

前面已经介绍了反斜线(\)有特殊的作用:它会转义,可以再字符串中加入通常情况下不能直接加入的内容。

例如换行符\n

>>> print 'Hello, \nworld!'
Hello,
world!

但有的时候,我们是希望字符串中就直接包含反斜线加n,例如\nowhere

>>> path = 'C:\nowhere'
>>> path
'C:\nowhere'
>>> print path
C:
owhere

可见打印出来的结果并不是我们想要的。我们想要的事C:\nowhere。

这种情况下,原始字符串就派上了用场。原始字符串不会把反斜线当做转义标记。原始字符串中的每个字符都和书写的方式保持一致。

>>> print r'C:\nowherer'
C:\nowherer
>>> print r'C:\Program Files\fnord\foo\bar'
C:\Program Files\fnord\foo\bar

可以看到,元是字符串以r开头。


注意:不能再原始字符串结尾输入反斜线。

>>> print r'hello,\'
SyntaxError: EOL while scanning string literal
>>>
因为Python不清楚这应该是结束字符串的意思还是要转义最后的引号的意思。

如果就是需要字符串的结尾时反斜线呢,那该如何做?

>>> print r'C:\Program Files\fnord\foo\bar\'
SyntaxError: EOL while scanning string literal


>>> print r'C:\Program Files\fnord\foo\bar' '\\'
C:\Program Files\fnord\foo\bar\
>>>
方法很多,这里介绍一种简单的方法,就是最后的反斜线单独处理。


input和raw_input的比较

1 >>> raw_input_A = raw_input("raw_input: ") 2 raw_input: abc 3  >>> input_A = input("Input: ") 4 Input: abc 5 6 Traceback (most recent call last): 7 File "<pyshell#1>", line 1, in <module> 8 input_A = input("Input: ") 9 File "<string>", line 1, in <module>10 NameError: name 'abc' is not defined11  >>> input_A = input("Input: ")12 Input: "abc"13  >>>

1 >>> raw_input_B = raw_input("raw_input: ")2 raw_input: 1233  >>> type(raw_input_B)4  <type 'str'>5 >>> input_B = input("input: ")6 input: 1237 >>> type(input_B)8 <type 'int'>9 >>>

例子 1 可以看到:这两个函数均能接收 字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。

而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。

例子 2 可以看到:raw_input() 将所有输入作为字符串看待,返回字符串类型。

而 input() 在对待纯数字输入时具有自己的特性,它返回所输入的数字的类型( int, float );


同时在例子 1 知道,input() 可接受合法的 python 表达式,举例:input( 1 + 3 ) 会返回 int 型的 4 。

查看 Built-in Functions ,得知:

input([prompt])

    Equivalent to eval(raw_input(prompt)) 

input() 本质上还是使用 raw_input() 来实现的,只是调用完 raw_input() 之后再调用 eval() 函数,所以,你甚至可以将表达式作为 input() 的参数,并且它会计算表达式的值并返回它。

不过在 Built-in Functions 里有一句话是这样写的:Consider using the raw_input() function for general input from users.

除非对 input() 有特别需要,否则一般情况下我们都是推荐使用 raw_input() 来与用户交互。


0 0