我是靠谱客的博主 矮小百褶裙,这篇文章主要介绍Python 基础之字符串string详解及实例,现在分享给大家,希望可以做个参考。

Python字符串(string) 详解 及 代码

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

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

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

具体参见代码注释;

代码如下:

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

输出:

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

最后

以上就是矮小百褶裙最近收集整理的关于Python 基础之字符串string详解及实例的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部