字符串拼接处理
字符串拼接
用加号拼接:
>>> a = 'he'+'llo' >>> a 'hello' >>> a = '你'+'好' >>> a '\xe4\xbd\xa0\xe5\xa5\xbd'
由于编码问题,中文没有正常显示,但可以正常打印(原因需要另外讨论):
>>> a = '你好' >>> print a 你好
python3在编码问题上做了改进,所以不会出现这种情况:
>>> a = '你好' >>> a '你好' >>> print(a) 你好
拼接时包含非字符串变量:
>>> name = 'Lily' >>> age = 23 >>> introduce = name + ' is ' + age + ' years old now.' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects
对于包含非字符串变量的情况也这样cubao地用加号连接肯定会惹来报错的(虽然在Java这是可行的)
正确姿势:
>>> introduce = name + ' is ' + str(age) + ' years old now.' >>> introduce 'Lily is 23 years old now.'
曾记否,有个强制转换叫
str()
,将变量强制转换为字符串就能用+
拼接了
打印输出时拼接
用加号
+
:>>> print('hello' + s + 'world') hellomyworld >>> print('hello ' + s + 'world') hello myworld >>> print('hello ' + s + ' world') hello my world
注意空格
用逗号
,
:>>> s = 'my' >>> print('hello ', s, ' world') hello my world >>> print('hello', s, 'world') hello my world >>> print('hello', s,'world') hello my world >>> print('hello',s,'world') hello my world
需要注意:使用
,
连接时将默认在连接处加一个空格,所以上面第一条语句的打印结果是每次有两个空格不用符号,打印多个字符串时会自动做拼接:
>>> print('hello ''my'' world') hello my world >>> print('hello''my'' world') hellomy world >>> print('hello''my''world') hellomyworld >>> print('hello' 'my' 'world') hellomyworld
如果有变量在里面就不行了:
>>> s = 'my' >>> print('hello' s 'world') File "<stdin>", line 1 print('hello' s 'world') ^ SyntaxError: invalid syntax
针对要求较多更复杂的情况,通常会使用下面介绍的 格式化字符串
字符串格式化
用%号
每个%s都会被后面括号里的参数依次替代,一一对应,字符串和后面的参数列表之间用百分号间隔
>>> print('%s is %d years old now.' % ('Lily', 23)) Lily is 23 years old now.
一般情况可以不用像C语言中区分%d,%s,%c的用法,python中可以都用%s,因为python一律将其作为字符串处理
>>> print('The charactor is %s; The number is %s and %s; The string is %s.' % ('A', 23, 3.1415926, 'hellomyword')) The charactor is A; The number is 23 and 3.1415926; The string is hellomyword.
用
%
格式化时也可以借助字典
对格式符进行命名
>>> print('%(name)s is %(age)d years old.' % {'name': 'Lisa', 'age': 24})
Lisa is 24 years old.
- 用.format()函数
>>> print('{} is {} years old.'.format('Lisa', 23))Lisa is 23 years old.
Lisa is 23 years old.
可以标记序号
>>> print('{0} is {1} years old.'.format('Lisa', 23))
Lisa is 23 years old.
>>> print('{1} is {0} years old.'.format('Lisa', 23))
23 is Lisa years old.
第二句话就显得有些滑稽了……
可以命名:
>>> print('{name} is {age} years old.'.format(name='Lisa', age=23))
Lisa is 23 years old.
- 以上全部特性同样适用于对独立的字符串进行格式化,如:
>>> s = '%(name)s is %(age)d years old.' % {'name': 'Lisa', 'age': 24}
>>> s
'Lisa is 24 years old.'
>>> s = '{name} is {age} years old.'.format(name='Lisa', age=23)
>>> s
'Lisa is 23 years old.'
格式化输出的修饰符
像C语言一样,格式符同样有转换说明,同样可以用修饰符进行修饰(修饰符主要用于修饰数字)
转换说明:
(因为正如前文所说,通常用%s格式化输出所有内容,所以其他转换说明用的比较少)
转换说明 | 输出 |
---|---|
%d | 输出十进制整数 |
%f | 输出浮点数 |
%c | 输出字符 |
%s | 输出字符串 |
修饰符:
修饰符 | 用途 |
---|---|
m.n | m表示字段总宽度,n表示小数点后的位数(精度) |
* | 定义宽度和精度 |
- | 左对齐 |
+ | 显示正负标识 |
# | 显示进制标识 |
0 | 前面用0填充 |
单引号 '
和双引号 "
的区别
在python里用起来没啥区别,但可以灵活运用处理一些情况:
>>> print('It\'s %s o\'clock' % ('17:00'))
It's 17:00 o'clock
字符串内部出现了单引号,但它并不是标志字符串开始或结束位置,这时候就需要用转义字符 \
将它转义
>>> print("It's %s o'clock" % ('17:00'))
It's 17:00 o'clock
但处理更便捷的一种方式是使用双引号 "
作为字符串的边界标志,避免了特殊符号的冲突