字符串
什么是字符串
GB2312:我国制定的中文编码,1个字节表示英文,2个字节表示中文.
UTF-8:1个字节表示英文,3个字节表示中文。
Python3.x:完全支持中文字符,默认使用UTF-8编码格式,无论是数字、英文、还是汉字,在统计字符串长度时都按一个字符处理
。
转义字符含义
转义字符 | 描述 |
---|---|
\ (在结尾时) | 续航符 |
\ \ | 反斜杠符号 \ |
\ ’ | 单引号 ‘ |
\ “ | 双引号 “” |
\a | 响铃 |
\b | 退格(Backspeace) |
\e | 转义 |
\000 | 空 |
\n | 换行 |
\v | 纵向制表符 |
\t | 横向制表符 |
\r | 回车 |
\f | 换页 |
\oyy | 八进制数yy代表字符 |
\xyy | 十六进制数yy代表字符 |
\other | 其他字符以普通形式输出 |
注意:为避免字符串中的字母被误解为转义字符,可以在字符串的前面加上R或者r表示原始字符串。
字符串输入输出方法
输出:
1.%
>>>name="python"
>>>height="未知"
>>>print ('我叫%s, 身高%s' %(name,height)) ** 传入的值为元组,依次填充
我叫python, 身高未知
2. format
顺序填坑:{} 占位符
>>>print('姓名是 {},年龄是 {}'.format('python',0))
姓名是:Tom,年龄是:0
下标填坑
>>>print('姓名是:{1},年龄是:{0}'.format(0,'python'))
姓名是:python,年龄是:0
变量填坑
1.函数中填充
>>>print('姓名是:{name},年龄是:{age}'.format(name='Python',age=0))
姓名是:Python,年龄是:0
2.提前定义变量
>>>name = "python"
>>>age = 0
>>>print('姓名是: {name},年龄是: {age}'.format(name=name,age=age)
姓名是: python,年龄是: 0
扩展:> 右对齐,< 左对齐, ^中间对齐
- str():函数返回一个用户易读的模式。
- reper():产生一个解释器易读的表达式。可以转义字符串中的特殊字符,参数可以是python任何对象。
输入:
inpput():接收一个python表达式输入并将运算结果返回。
>>>i=input("请输入:"); **假设输入的是12345
>>>print(i)
12345
“”“多行字符串”"":三个引号可以让字符串跨行使用。
格式化操作符
格式化操作符号
符号 | 描述 |
---|---|
%c | 格式化字符及其ASCII码 |
%s | 格式化字符串 |
%d | 格式化整数 |
%u | 格式化无符号整型 |
%o | 格式化无符号八进制数 |
%x | 格式化无符号十六进制整数 |
%X | 格式化无符号十六进制数(大写) |
%f | 格式化浮点数字,可指定小数点后的精度 |
%e | 用科学计数法格式化浮点数 |
%E | 同%e |
%g | %f和%e简写 |
%G | %F和%E简写 |
%p | 用十六进制格式化变量的地址 |
格式化操作符辅助指令
符号 | 功能 |
---|---|
* | 定义宽度或者小数点精度 |
- | 左对齐 |
+ | 正数前面显示加号 |
< sp > | 正数面前显示空格 |
# | 八进制,十六进制前显示’o‘、’ox‘/’OX |
O | 显示的数字前面填充’o‘ |
%% | 输出’%‘ |
(var) | 映射变量 |
m.n. | 前者是显示的最小总宽度,后者是小数点后的数 |
存储方式
字符串特性:不可变序列,支持短字符串驻留机制,不支持长字符串驻留机制。
短字符串驻留机制:当一个值赋给多个不同的变量(对象)时,内存只有一个副本,多个变量(对象)访问同一个副本。
>>>a='123'
>>>b='456'
>>>id(a)==id(b)
True
>>>a='123'*50
>>>b='456'*50
>>>id(a)==id(b)
False
切片截取字符
工具:varl[ ]
>>>varl='Hello word!'
>>>print("varl[0]:",varl[0])
>>>print("varl[7]:",varl[7]) ** 说明空格也算一个切割位
varl[0]: H
varl[7]: o
字符串内建立函数的目的
find/rfind
find():首次出现位置(+1才是真正在的地方)
rfind():最后一次出现位置(+1才是真正在的地方)
>>> s="asasdas"
>>> s.find("s")
1
>>> s.rfind("s") #
6
index/rindex
index():首次出现位置(+1才是真正在的地方)
rindex():最后一次出现位置(+1才是真正在的地方)
>>> s="asasdas"
>>> s.index("s")
1
>>> s.rindex("s")
6
计数 count
>>> s="asasdas"
>>> s.count("s")
3
查找替换 replace
>>> s="中国,中国"
>>> s2=s.replace("中国","中华人民共和国")
>>> print(s2)
中华人民共和国,中华人民共和国
splite
自动分隔.split()
>>> s="\n\nhello\t\t world\n\n\n My name is Dong"
>>> s.split() #自动分隔
['hello', 'world', 'My', 'name', 'is', 'Dong']
指定分割次数.split(None,X)
>>> s="\n\nhello\t\t world\n\n\n My name is Dong"
>>>> s.split(None,1) #指定分割次数1
['hello', 'world\n\n\n My name is Dong']
>>> s.split(None,2) #指定分割次数2
['hello', 'world', 'My name is Dong']
>>> s.split(None,3) #指定分割次数3
['hello', 'world', 'My', 'name is Dong']
>>> s.split(None,4) #指定分割次数4
['hello', 'world', 'My', 'name', 'is Dong']
>>> s.split(None,5) #指定分割次数5
['hello', 'world', 'My', 'name', 'is', 'Dong']
限制分割次数.split(maxsplit=X)
>>> s="\n\nhello\t\t world\n\n\n My name is Dong"
>>>> s.split(maxsplit=6) # 限制分割次数6
['hello', 'world', 'My', 'name', 'is', 'Dong']
>>> s.split(maxsplit=5) # 限制分割次数5
['hello', 'world', 'My', 'name', 'is', 'Dong']
>>> s.split(maxsplit=4) # 限制分割次数4
['hello', 'world', 'My', 'name', 'is Dong']
>>> s.split(maxsplit=3) # 限制分割次数3
['hello', 'world', 'My', 'name is Dong']
>>> s.split(maxsplit=2) # 限制分割次数2
['hello', 'world', 'My name is Dong']
>>> s.split(maxsplit=1) # 限制分割次数1
['hello', 'world\n\n\n My name is Dong']
指定分隔符
>>> s="\n\nhello\t\t world\n\n\n My name is Dong"
>>> s.split("\n") #指定分隔符\n
['', '', 'hello\t\t world', '', '', ' My name is Dong']
大小写综合
变小写.lower()
>>> s="What is Your Name?"
>>> s.lower()
'what is your name?'
变首字母大写.capitalize()
>>> s="What is Your Name?"
>>> s.capitalize()
'What is your name?'
大小写互换.swapcase()
>>> s="What is Your Name?"
>>> s.swapcase()
'wHAT IS yOUR nAME?'
每个单词首字母变大写.title()
>>> s="What is Your Name?"
>>>> s.title()
'What Is Your Name?'
全部大写
>>> s="What is Your Name?"
>>> s.upper()
'WHAT IS YOUR NAME?'
判断字符开头.startswith(" ")
>>> s="What is Your Name?"
>>> s.startswith("W")
True
判断字符结尾.endswith (" ")
>>> s="What is Your Name?"
>>> s.endswith ("W")
False
字符串对齐函数
中心.center()
>>> s.center(30,"*")
'******What is Your Name?******'
左.ljiust()
>>> s.ljust(30,"*")
'What is Your Name?************'
右.ljiust()
>>> s.rjust(30,"*")
'************What is Your Name?'
去字符串函数
删除空白字符strip()
>>> " abc ".strip()
'abc'
删除右边指定字符rstrip()
>>> "aaaaabcaaaaa".rstrip("a")
'aaaaabc'
删除左边指定字符lstrip()
>>> "aaaaabcaaaaa".lstrip("a")
'bcaaaaa'
字符串运算符
加法运算
>>> "hello" +"hello"
'hellohello'
关键字in
>>> "e" in "hello"
True
>>> "i" in "hello"
False
乘法运算
>>> "hello" *3
'hellohellohello'