当前位置:Gxlcms > Python > Python基础中的字符串详解

Python基础中的字符串详解

时间:2021-07-01 10:21:17 帮助过:59人阅读

Python的字符串可以使用单引号('), 双引号("), 三引号('''); 三引号(''')里面, 可以添加单引号和双引号, 也可以通过转义序列(\)添加;
字符串放在一起自动连接成为一个字符串;

字符串前面添加限定词R或r, 表示是自然字符串(nature string), 可以忽略里面的格式限制;

在物理行末尾添加"\", 可以连接下一个物理行; 括号, 方括号, 大括号也可以一定限度的扩充物理行;

具体参见代码注释;

  1. # -*- coding: utf-8 -*-
  2. #====================
  3. #File: abop.py
  4. #Author: Wendy
  5. #Date: 2013-12-03
  6. #====================
  7. #eclipse pydev, python3.3
  8. #三引号可以自由的使用双引号("")和单引号('')
  9. s = ''''' I am a girl and like heels.
  10. Hello, "python" sister. '''
  11. #转义序列"\"
  12. st = '''''Hello, girls, l like (\'''). '''
  13. #字符串放在一起自动连接
  14. sa = 'Girls are ''the best. '
  15. #r表示自然字符串, 不会进行转义(\n)
  16. sr = r"nature is good. \n {0}"
  17. #"\"表示连接字符串
  18. sc = 'Oh, the lines are too \
  19. large'
  20. #括号, 方括号, 大括号, 可以限定范围, 不用使用转义
  21. print("the braces can do {thing}.".
  22. format(thing="lady"))
  23. print(s)
  24. print(st)
  25. print(sa)
  26. print(sr)
  27. print(sc)

输出:

  1. the braces can do lady.
  2. I am a girl and like heels.
  3. Hello, "python" sister.
  4. Hello, girls, l like (''').
  5. Girls are the best.
  6. nature is good. \n {0}
  7. Oh, the lines are too large

以上就是Python基础中的字符串详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行