Python基础(二)之数据类型和运算(2)——字符串
字符串基础
Python 也提供了可以通过几种不同方式表示的字符串。它们可以用单引号 ('...'
) 或双引号 ("..."
) 标识 。\
可以用来转义引号:
>>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," he said.' '"Yes," he said.' >>> "\"Yes,\" he said." '"Yes," he said.' >>> '"Isn\'t," she said.' '"Isn\'t," she said.'
在交互式解释器中,输出的字符串会用引号引起来,特殊字符会用反斜杠转义。虽然可能和输入看上去不太一样,但是两个字符串是相等的。如果字符串中只有单引号而没有双引号,就用双引号引用,否则用单引号引用。print() 函数生成可读性更好的输出, 它会省去引号并且打印出转义后的特殊字符:
>>> '"Isn\'t," she said.' '"Isn\'t," she said.' >>> print('"Isn\'t," she said.') "Isn't," she said. >>> s = 'First line.\nSecond line.' # \n means newline >>> s # without print(), \n is included in the output 'First line.\nSecond line.' >>> print(s) # with print(), \n produces a new line First line. Second line.
如果你前面带有 \
的字符被当作特殊字符,你可以使用 原始字符串,方法是在第一个引号前面加上一个 r
:
>>> print('C:\some\name') # here \n means newline! C:\some ame >>> print(r'C:\some\name') # note the r before the quote C:\some\name
字符串多行输出
字符串文本能够分成多行。一种方法是使用三引号:"""..."""
或者 '''...'''
。行尾换行符会被自动包含到字符串中,但是可以在行尾加上 \
来避免这个行为。下面的示例: 可以使用反斜杠为行结尾的连续字符串,它表示下一行在逻辑上是本行的后续内容:
1 print("""\ 2 Usage: thingy [OPTIONS] 3 -h Display this usage message 4 -H hostname Hostname to connect to 5 """)
将生成以下输出(注意,没有开始的第一行):
Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
字符串格式化输出
字符串可以由 +
操作符连接(粘到一起),可以由 *
表示重复:
>>> # 3 times 'un', followed by 'ium' >>> 3 * 'un' + 'ium' 'unununium'
相邻的两个字符串文本自动连接在一起。:
>>> 'Py' 'thon' 'Python'
它只用于两个字符串文本,不能用于字符串表达式:
>>> prefix = 'Py' >>> prefix 'thon' # can't concatenate a variable and a string literal ... SyntaxError: invalid syntax >>> ('un' * 3) 'ium' ... SyntaxError: invalid syntax
如果你想连接多个变量或者连接一个变量和一个字符串文本,使用 +
:
>>> prefix + 'thon' 'Python'
这个功能在你想切分很长的字符串的时候特别有用:
>>> text = ('Put several strings within parentheses ' 'to have them joined together.') >>> text 'Put several strings within parentheses to have them joined together.'
字符串拼接格式化输出
name = input("name:") age = input("age:") job = input("job:") print(""" -------------info----------- name:"""+name+""" age:"""+age+""" job:"""+job)
输入
name:Brain age:23 job:IT
输出
-------------info----------- name:Brain age:23 job:IT
使用占位符格式化输出(一)
1 name = input("name:") 2 age = input("age:") 3 job = input("job:") 4 salary = input("salary:") 5 6 info = ''' 7 ------------INFO OF %s------------ 8 name:%s 9 age:%s 10 job:%s 11 salary:%s 12 ''' %(name,name,age,job,salary) 13 14 print(info)
输入
name:yang
age:23
job:IT
salary:2345
输出
-----------INFO OF yang------------
name:yang
age:23
job:IT
salary:2345
PS: 字符串是 %s;整数 %d;浮点数%f
name,salary为字符串类型,可以强制转换成整型和浮点型,由于Python是强数据类型,此处需要强制转换
age = int(input("age:") ) salary = float(input("salary:"))
注意:1、占位符需要对应改为整数 %d;浮点数%f
2、%d、%f 混用不会报错,%d 省略小数,%f 补全小数
强制转换成字符型
name = str( input("name:") ) print(type(name)) #输出变量数据类型
使用占位符格式化输出(二)
1 name = input("name:") 2 age = int(input("age:") ) 3 job = input("job:") 4 salary = float(input("salary:")) 5 6 info2 = ''' 7 ------------INFO OF {_name}------------ 8 name:{_name} 9 age:{_age} 10 job:{_job} 11 salary:{_salary} 12 ''' .format(_name=name, #赋值 13 _age=age, 14 _job=job, 15 _salary=salary) 16 17 print(info2)
使用占位符格式化输出(三)
1 name = input("name:") 2 age = int(input("age:") ) 3 job = input("job:") 4 salary = float(input("salary:")) 5 6 info3 = ''' 7 ------------INFO OF {0}------------ 8 name:{0} 9 age:{1} 10 job:{2} 11 salary:{3} 12 ''' .format(name,age,job,salary) 13 14 print(info3)
字符串截取(检索)
字符串也可以被截取(检索)。类似于 C ,字符串的第一个字符索引为 0 。Python没有单独的字符类型;一个字符就是一个简单的长度为1的字符串。:
>>> word = 'Python' >>> word[0] # character in position 0 'P' >>> word[5] # character in position 5 'n'
索引也可以是负数,这将导致从右边开始计算。例如:
>>> word[-1] # last character 'n' >>> word[-2] # second-last character 'o' >>> word[-6] 'P'
请注意 -0 实际上就是 0,所以它不会导致从右边开始计算。
除了索引,还支持 切片。索引用于获得单个字符,切片 让你获得一个子字符串:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' >>> word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho'
注意,包含起始的字符,不包含末尾的字符。这使得 s[:i] + s[i:]
永远等于 s
:
>>> word[:2] + word[2:] 'Python' >>> word[:4] + word[4:] 'Python'
切片的索引有非常有用的默认值;省略的第一个索引默认为零,省略的第二个索引默认为切片的字符串的大小。:
>>> word[:2] # character from the beginning to position 2 (excluded) 'Py' >>> word[4:] # characters from position 4 (included) to the end 'on' >>> word[-2:] # characters from the second-last (included) to the end 'on'
有个办法可以很容易地记住切片的工作方式:切片时的索引是在两个字符 之间 。左边第一个字符的索引为 0,而长度为 n 的字符串其最后一个字符的右界索引为 n。例如:
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
文本中的第一行数字给出字符串中的索引点 0…6。第二行给出相应的负索引。切片是从 i 到 j 两个数值标示的边界之间的所有字符。
对于非负索引,如果上下都在边界内,切片长度就是两个索引之差。例如,word[1:3]
是 2
试图使用太大的索引会导致错误。
Python字符串不可以被更改 — 它们是 不可变的 。因此,赋值给字符串索引的位置会导致错误:
>>> word[0] = 'J' ... TypeError: 'str' object does not support item assignment >>> word[2:] = 'py' ... TypeError: 'str' object does not support item assignment
如果你需要一个不同的字符串,你应该创建一个新的:
>>> 'J' + word[1:] 'Jython' >>> word[:2] + 'py' 'Pypy'
内置函数 len() 返回字符串长度:
>>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34