第一章、基础知识(Part2)--字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leaf5022/article/details/37934033

第一章、基础知识(Part2)--字符串

python


1.11字符串

单引号字符串和转义引号

与shell一样,将字符串括起来可以使用单引号对也可以使用双引号对。不同之处在于字符串中带有引号的情况。

注意:在以下例子中Python,用双引号括起来的语句,打印结果使用单引号括起来,反过来用单引号括起来的语句,打印结果使用双引号括起来。

 
 
  1. >>> "let's go!"
  2. "let's go!"
  3. >>> '"hello world",she said'
  4. '"hello world",she said'
  5. >>> 'hello world'
  6. 'hello world'
  7. >>> "hello world"
  8. 'hello world'

为了便于区分,我们可以把表示所有格(例如let's)的单引号叫做撇号。因为将字符串括起来得引号不论是单引号还是双引号都是成对出现的,所以有时需要两种引号混用才能让解释器正确解释。但也可以使用反斜杠(\)来转义引号。

 
 
  1. >>> 'let\'s go'
  2. "let's go"
  3. >>> "\"Hello!\" she said"
  4. '"Hello!" she said'

注意:使用长字符串和原始字符串两者联合使用可以减少绝大多数的反斜杠的使用。

拼接字符串

使用加好(+)可以将字符串拼接起来。

 
 
  1. >>> x="hello"
  2. >>> y="world"
  3. >>> x+y
  4. 'helloworld'
  5. >>> x+ y
  6. 'helloworld'

如果想要让hello 和 world之间有一个空格,那么应当让x="hello "(在末尾有个空格) 。

字符串表示,str 和 repr

通过前面的例子可以可以看出通过Python打印的字符串还是会被引号括起来。这是因为Python打印值的时候会保持该值在Python代码中的状态。但是通过Print打印的情况就不一样。

 
 
  1. >>> print "hello"
  2. hello
  3. >>> "hello"
  4. 'hello'
  5. >>> 100000000L
  6. 100000000L
  7. >>> print 1000000L
  8. 1000000

Python中值被转为字符串有两种机制:str函数将值转换为合理形式的字符串,以便理解;而repr函数会创建一个字符串,它以合法的Python表达式的形式来表示值。

 
 
  1. >>> print repr("hello")
  2. 'hello'
  3. >>> print repr(100000L)
  4. 100000L
  5. >>> print str("hello")
  6. hello
  7. >>> print str(100000L)
  8. 100000

repr(x)的功能也可以用x实现(`是反引号)。

 
 
  1. >>> temp=42
  2. >>> print "The meaning of life is" + temp
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in <module>
  5. TypeError: cannot concatenate 'str' and 'int' objects
  6. >>> print "The meaning of life is " + `temp`
  7. The meaning of life is 42

第一个print无法工作是因为不能使用加好将字符串和数字进行连接,而第二个print将temp转为字符串"42"。

注意:在Python3.0中已经不再使用反引号了,因此即使在旧的代码中看到了反引号,也应该矜持使用repr。

input 和 raw_input的比较

使用input时,输入的内容必须是合法的Python表达式,例如字符串必须带有引号对等。而raw_input将所有的输入当作原始数据(raw data),然后放入字符串中:

 
 
  1. >>> input("Enter a number: ")
  2. Enter a number: 3
  3. 3
  4. >>> raw_input("Enter a number: ")
  5. Enter a number: 3
  6. '3'

除非对input有特别的需要,否则应该尽可能使用raw_input函数。

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

1、长字符串

可以使用两头各三个引号(单引号或双引号)字符串括起来,这样可以跨行书写,这之中大的单引号和双引号就不需要反斜杠来转义。也可以使用反斜杠来将换行符转义,来实现跨行。

 
 
  1. print '''This is a very long string.
  2. It continues here.
  3. And it's not over yet.
  4. "Hello, world!"
  5. Still here.'''
  6. print "Hello,\
  7. world!"
2、原始字符串

字符串中有反斜杠时,有些符号会被转义,如:\n表示换行符,\t表示跳到下一个tab位置,\表示转义反斜杠自身还有引号等。
原始字符串:在字符串前加个r。在原始字符串中输入的每个字符都会与书写方式保持一致。
注意:不能在原始字符串结尾输入反斜杠,如果需要最后一个字符是反斜杠,则要对其进行转义。
可在原始字符串中同时使用单双引号,即使三引号字符串也可以充当原始字符串。

 
 
  1. #字符串中的反斜杠后的有些字符会被转义
  2. >>> print 'c:\Program Files\nfoo\tfoo\foo'
  3. c:\Program Files
  4. foo foo
  5. oo
  6. #原始字符串将字符串的每个字符原样保持
  7. >>> print r'c:\Program Files\nfoo\tfoo\foo'
  8. c:\Program Files\nfoo\tfoo\foo
  9. #原始字符串末尾不能是反斜杠
  10. >>> print r'c:\Program Files\nfoo\tfoo\foo\'
  11. File "<stdin>", line 1
  12. print r'c:\Program Files\nfoo\tfoo\foo\'
  13. ^
  14. SyntaxError: EOL while scanning string literal
  15. #如果需要末尾为反斜杠的情况的解决技巧
  16. >>> print r'c:\Program Files\nfoo\tfoo\foo' '\\'
  17. c:\Program Files\nfoo\tfoo\foo\
  18. #可以使用单,双、三引号对
  19. >>> print r"c:\Program Files\nfoo\tfoo\foo"
  20. c:\Program Files\nfoo\tfoo\foo
  21. >>> print r'''c:\Program Files\nfoo\tfoo\foo'''
  22. c:\Program Files\nfoo\tfoo\foo
  23. #原始字符串中的引号不被转义
  24. >>> print r'Let\'s go'
  25. Let\'s go
3、Unicode

如果需要使用Unicode字符串,在字符串前加上u即可。Unicode字符串,也称为Unicode对象,与字符串并不是同一类型。

 
 
  1. >>> u'hello world'
  2. u'hello world'
  3. >>> r'hello world'
  4. 'hello world'

猜你喜欢

转载自blog.csdn.net/leaf5022/article/details/37934033
今日推荐