字符串

作者:追风剑情 发布于:2017-12-3 14:55 分类:Python

  1. # -*- coding: cp936 -*-
  2. print "Python中字符串支持单引号或者双引号,也支持转义符"
  3. print "Hello. world!"
  4. print 'Hello. world!'
  5. print "Let's go!"
  6. print 'Let\'s go!'
  7. print "\"Hello. world!\" she said"
  8.  
  9. print "拼接字符串"
  10. print "Let's say " '"Hello. world!"'
  11. x = "Hello."
  12. y = "world!"
  13. print x + y
  14.  
  15. print "str函数、repr函数"
  16. print repr("Hello. world!")
  17. print repr(10000L)
  18. print str("Hello. world!")
  19. print str(10000L)
  20.  
  21. #字符串连接数字时,数字变量要用`(不是单引号)括起来
  22. #str和int、long一样,是一种类型。而repr仅仅是函数
  23. temp = 42
  24. print "The temperature is " + `temp`
  25. print "The temperature is " + repr(temp)
  26.  
  27. #input与raw_input的区别
  28. #input会假设用户输入的是合法的Python表达式
  29. #raw_input会把所有输入当作原始数据
  30. #除非有特殊需求,否则应该尽可能使用raw_input函数
  31.  
  32. print "长字符串"
  33. #用三个单引号或才三个双引号括起来
  34. print '''This is a very long string.
  35. It coutinues here.
  36. And it's not over yet.
  37. "Hello. world!"
  38. Still here.'''
  39.  
  40. #普通字符串也可以跨行。如果一行之中最后一个字符是反斜线,那么,换行符本
  41. #身就"转义"了,也就是被忽略了。
  42. print "Hello. \
  43. world."
  44. #这种对换行符转义的方式也适用于表达式
  45. 1 + 2 + \
  46. 4 + 5
  47. print \
  48. 'Hello. world'
  49.  
  50. print "原始字符串"
  51. print "C:\\Program Files\\Microsoft"
  52. #在字符串前加个r,后面的内容就会忽略转义符\
  53. print r"C:\Program Files\Microsoft"
  54. #如果想在字符串的最后显示反斜线
  55. print r"C:\Program Files\Microsoft" '\\'
  56.  
  57. print "表示Unicode字符串"
  58. #在前面加个u(注意:在Python3.0中所有的字符串都是Unicode)
  59. print u'Hello. world!'

运行测试

1111.png

标签: Python

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号