1、 字符串的创建
- 单行字符串:一对单引号或双引号
- 多行字符串:三个单引号或双引号
>>> a='hello world'
>>> a
'hello world'
>>> b="hello world"
>>> b
'hello world'
>>> c='''hello
... world'''
>>> c
'hello\nworld'
>>> print(c)
hello
world
>>> d="""hello
... world
... hello"""
>>> d
'hello\nworld\nhello'
>>> print(d)
hello
world
hello
>>>
2、类型转换
使用str()可以将其他类型转换为字符串,调用print()函数时,Python内部会自动将非字符串对象转换为字符串
>>> str(3.14)
'3.14'
>>> str(1.0e4)
'10000.0'
>>> str(True)
'True'
>>> a=(1,2,3,4)
>>> str(a)
'(1, 2, 3, 4)'
>>>
3、字符串运算符
(1) 字符串拼接 +
使用+将多个字符串拼接起来,也可以直接将一个字面字符串放到另一个后面实现直接拼接。
>>> a='hello '
>>> b='world'
>>> a+b
'hello world'
>>> 'hello ' 'world'
'hello world'
>>>
(2)字符串复制 *
使用*可以实现字符串复制
>>> a='hello '
>>> a*4
'hello hello hello hello '
>>>
(3)通过索引获取字符 []
在字符串后面添加[],并在括号中给定偏移量,来提取字符串该位置处的字符。
**索引值有两种形式:
1、索引为0或者正数,从左往右,0为最左端
2、索引为负数,从右往左,-1为最右端**
>>> a='abcdefghi'
>>> a[0]
'a'
>>> a[5]
'f'
>>> a[-1]
'i'
>>> a[-3]
'g'
>>>
(4)分片操作[start,end,step]
使用起始偏移量start,终止偏移量end,可选的步长来定义一个分片,偏移量从左往右从0开始依次增加,从右往左从-1开始依次减小。
- [:]提取从开头到结尾的整个字符串
- [start:]从start提取到结尾
- [:end]从开头提取到end-1
- [start:end]从start提取到end-1
- [start:end:step]从start提取到end-1,每step个字符提取一个
>>> a='abcdefghijklmnopqrstuvwxyz'
>>> a[:]
'abcdefghijklmnopqrstuvwxyz'
>>> a[20:]
'uvwxyz'
>>> a[:10]
'abcdefghij'
>>> a[10:20]
'klmnopqrst'
>>> a[2:-2:3]
'cfilorux'
>>> a[-3:]
'xyz'
>>> a[15:-5]
'pqrstu'
>>>
(5)字符串长度 len()
使用len()计算字符串包含的字符数
>>> a='hello'
>>> len(a)
5
>>> len('你好')
2
>>>
(6)字符串分割 split()
split()可以基于某个分隔符将字符串分割成若干个子串组成的列表(list)
函数参数为分隔符,若不指定分隔符,默认使用空白字符——换行、空格、制表符
>>> a='zhangsan,lisi,wangwu'
>>> a.split(',')
['zhangsan', 'lisi', 'wangwu']
>>> a.split()
['zhangsan,lisi,wangwu']
>>> b='zhangsan lisi wangwu'
>>> b.split()
['zhangsan', 'lisi', 'wangwu']
>>> b.split(',')
['zhangsan lisi wangwu']
>>>
(7)字符串列表合并 join()
join()与split()正好相反,用于将一个字符串列表中的字符串按照特定的分隔符合并成一个字符串,参数为待合并的列表
注意:必须制定分隔符,如果没有分隔符可以指定为空串“”或‘’
>>> a=['zhangsan','lisi','wangwu']
>>> ' '.join(a)
'zhangsan lisi wangwu'
>>> '#'.join(a)
'zhangsan#lisi#wangwu'
(8)子串替换 replace()
使用replace()可以进行简单的子串替换,传入参数包括:需要被替换的子串,用于替换的新子串,以及可选的需要替换多少处,若不指定替换多少处,则默认全部替换,否则替换次数应不超过指定的替换次数
>>> a='aaaaaaaaaa'
>>> a.replace('a','b')
'bbbbbbbbbb'
>>> a.replace('a','b',3)
'bbbaaaaaaa'
>>> a.replace('a','b',1)
'baaaaaaaaa'
>>> a.replace('a','b',100)
'bbbbbbbbbb'
>>>
(9)其他常用方法
函数 | 描述 |
---|---|
starstwith() | 是否以某个子串开头 |
endswith() | 是否以某个子串结束 |
find() | 子串第一次出现的位置 |
rfind() | 子串最后一次出现的位置 |
count() | 子串出现的次数 |
isalnum() | 所有字符是否都是字母或数字 |
capitalize() | 字符串首字母大写 |
title() | 所有单词开头字母大写 |
upper() | 所有字母都变大写 |
lower() | 所有字母都变小写 |
>>> a='there was a Young Lady of Norway'
>>> a.startwith('there')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'startwith'
>>> a.startswith('there')
True
>>> a.endswith('way')
True
>>> a.find(' ')
5
>>> a.rfind('a')
30
>>> a.count('a')
4
>>> a.isalnum()
False
>>> "dfjghdfh123".isalnum()
True
>>> a.capitalize()
'There was a young lady of norway'
>>> a.title()
'There Was A Young Lady Of Norway'
>>> a.upper()
'THERE WAS A YOUNG LADY OF NORWAY'
>>> a.lower()
'there was a young lady of norway'
>>>
4、字符串格式化
Python中提供格式化字符串的输出,使用与C中基本一样的语法,使用见示例:
>>> print('我叫%s,今年%d岁'%('小明',10))
我叫小明,今年10岁
>>>