【原创】Python第二章——字符串

    字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符。Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而不是修改现有的字符串。

 
字符串常量的表示
 
1. 3种表示
1 #单引号
2 A = 'Python'
3 #引号
4 B = "Python"
5 #三引号
6 C = """Python""" 
2. 为什么要这么麻烦?
 
(1)当字符串中包含了某种引号字符,那么使用与该引号字符不同的引号包含字符串时,可以直接使用该引号,而不用使用转义字符
1 #单引号不用转义
2 A = "I'm a Python newcomer"
3 #引号不用转义
4 B = 'I have learnt "C" language'

 

(2)三引号可以让输出的字符串直接跨行
 
当没有三引号时,不得不这样做:
A = "I'm \n\
a newcomer"

 

如果使用三引号,可以不用使用\n\
B = """I'm a
newcomer"""

 

3. 过长字符串的跨行表示
 
建议使用圆括号将多行字符串包括在一起,而不是用转义符。
1 #建议
2 A = ("I'm "
3     "a newcomer")
4 #不建议
5 B = "I'm " + \
6     "a newcomer"
7 C = "I'm \
8      a newcomer"
4. 转义序列
 
转义字符 含义
\newline 忽略换行
\\ \
\' '
\" "
\a ASCII蜂鸣
\b ASCII退格
\f ASCII走纸
\r 回车CR
\n 换行LF
\t ASCII制表符
\v ASCII垂直指标
\ooo 给定八进制字符
\xhh 给定8位十六进制字符
\uhhhhh 给定16位十六进制字符
\Uhhhh hhhh 给定32位十六进制字符
\N{name} 给定名称的Unicode字符,name是一个标准Unicode名称
字符串的比较
 
1. < <= > >= == !=与C语言的库函数相似
 
2. Unicode字符比较存在的问题
(1)有些Unocde字符可以使用多种字节序列表示,比如某些字符可以使用3中不同的UTF-8编码字节表示。
解决方法:使用unicodedata模块的normalize函数,并将'NKFD'作为该函数的第一个实参。
1 import unicodedata
2 title = u"Klüft skräms inför på fédéral électoral große"
3 B = unicodedata.normalize('NFKD', title).encode('ascii','ignore') 
4 print(B)

 

(2)字符排序问题
在一些国家中,字母字符的顺序不同于英美国家,例如在瑞典语中,a排在z之后。对此Python不进行推测,而是使用字符串的内存字节进行比较,此时的排序是基于Unocode字元的,当然我们也可以自定义Python的排序方法。
 
字符串的提取——分片
    这和序列的提取——分片是一样的,这放在下一篇文章讲。
 
字符串操作符和方法
 
1. 操作符
(1)比较操作符< <= > >= == !=与C语言的库函数相似
(2)*(复制),*=,+(追加),+=
(3)成员关系测试 in
 
2. 方法
    字符串实现了所以通用序列的方法,也带有自己特别的方法,详见帮助文档,这里不说。
(1)格式化字符串的两种方法
 
printf风格字符串格式化
    %操作符,用于格式化的一个操作符。形式为:
  1. format % values
其中format是一个格式化字符串,%用于将后面的values替换前面的格式字符串,作用和C语言的sprintf一样。
在format字符串中带有一个以%开头的转换标识符。用于指示后面value如何替换前面的format字符串。
 
1 print('number is %d'%10) 
2 将输出number is 10

 

其中的%d就是一个转换标识符,表示这里是一个整数,%d被替换成真实值10。
 
转换标识符不仅仅一种,它包含两个或以上的字符,并且顺序化地包含以下内容:(提醒:顺序是固定的,但有的是可选)
1) 必须以%开头
2)Mapping key(可选),由一对括号包含的字符序列
3)转换标志(可选),作用于一些转换类型的结果
4)最小宽度字段(可选),用于使转换标识符最后转换得到的长度达到指定的最小宽度字段
5)精度(可选),含义取决于格式字符
6)长度修饰符(可选),修饰整数
7)转换格式字符(必须)
 

 

转换标志有:
FlagMeaning
'#' 转换标志的起始符
'0' 用0在数的字段宽度内进行填充
'-' 左对齐 (如果给出了上面的0转换和左对齐转换,则忽略0转换
' ' (一个空格)在一个有符号转换中,如果是一个正数,那么在开头加上空格而不是'+'
'+' 若数为正数,则在开头添加'+'(忽略空格标志).
最小宽度字段:
    如果字符的数量小于指定的最小宽度字段,则会在剩余的地方进行填充(默认是空格,可以由转换标志修改填充的字符);如果大于,则原样输出。
 
精度:含义取决于格式字符
 
格式字符含义
dioux 精度表示最少显示数字位数,如果精度过大则以0填充,否则原样输出
ef 精度表示小数点后的数字位数
g 精度表示最大有效位
s 精度表示最大的字符数
   
长度修饰符:
h,l,L
h:说明整数是short型
l:说明整数是long型
L:当和efg使用时,L说明是long double型
 

The conversion types are:

ConversionMeaningNotes
'd' Signed integer decimal.  
'i' Signed integer decimal.  
'o' Signed octal value. (1)
'u' 已废弃,等同于d (7)
'x' Signed hexadecimal (lowercase). (2)
'X' Signed hexadecimal (uppercase). (2)
'e' Floating point exponential format (lowercase). (3)
'E' Floating point exponential format (uppercase). (3)
'f' Floating point decimal format. (3)
'F' Floating point decimal format. (3)
'g' Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)
'G' Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)
'c' Single character (accepts integer or single character string).  
'r' String (converts any Python object using repr()). (5)
's' String (converts any Python object using str()). (5)
'a' String (converts any Python object using ascii()). (5)
'%' 输出%  

Notes:

  1. The alternate form causes a leading zero ('0') to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.当使用了转换标志0,那么填充0的时候,填充的位置将是0o和数字之间,比如%#08o' % 34的结果是0o000042

  2. The alternate form causes a leading '0x' or '0X' (depending on whether the 'x' or 'X' format was used) to be inserted between left-hand padding and the formatting of the number if the leading character of the result is not already a zero.当使用了转换标志0,那么填充0的时候,填充的位置将是0x和数字之间,比如%#08x' % 34的结果是0x000022。

  3. The alternate form causes the result to always contain a decimal point, even if no digits follow it.

    The precision determines the number of digits after the decimal point and defaults to 6.精度控制决定了小数点后的位数,默认是6位

  4. The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.

    The precision determines the number of significant digits before and after the decimal point and defaults to 6.

  5. If precision is N, the output is truncated to N characters.如果上面指定的精度是N,那么字符串的最大长度是N

  1. See PEP 237.

Since Python strings have an explicit length, %s conversions do not assume that '\0' is the end of the string.

因为Python的String有显式的长度,所以%s转换不认为'\0'是字符串的结尾

Changed in version 3.1: %f conversions for numbers whose absolute value is over 1e50 are no longer replaced by %g conversions.

posted @ 2015-08-11 21:07  cposture  阅读(1178)  评论(0编辑  收藏  举报
levels of contents