·字符串(string)

笔记:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

Python中用引号括起来的都是字符串,可以是单引号也可以是双引号

 This is a string

 This is a string

 

@ 字符串的基本操作

在序列中所有标准操作(索引、乘法、成员资格、长度、最小值、最大值)都适用字符串,然而,字符串是不可变的,所以赋值是非法的

h = 'hello,world!'
h[0] = 'p'
print(h)
打印结果: Traceback (most recent call last): File "test1.py", line 3, in <module>
    h[0] = 'p'
TypeError: 'str' object does not support item assignment #字符串不支持赋值

 

@字符串的方法

 字符串方法集合:

hello = '    this is my favorite    '
print(hello)
print('title()将字符串首字母替换成大写字母:',hello.title()) 
print('upper()将整体字符串字母替换成大写字母:',hello.upper()) 
print('lower()将整体字符串字母替换成小写字母:',hello.lower()) 
print('lstrip()删除字符串前的空白:',hello.lstrip())
print('rstrip()删除字符串后的空白:',hello.rstrip())
print('strip()删除字符串前、后的空白:',hello.strip())
print('find()查找子串的索引:',hello.find('t'))       #空格也有索引
print('center()在字符串两边填充字符(默认空格)并居中:',hello.center(31,'*'))
#split()括号中没有指定分割符,将默认在单个或多个空白字符(空格、制表符、换行符等)处进行拆分
x = hello.split()
print('split()将字符串装换为列表',x) 
fo = '+' #指定分割符
y = fo.join(x)
print('join()将列表转换为字符',y)  
print(y)
print('replace()替换指定字符串:',y.replace('+','-'))
po = str.maketrans('timf','TIMF',' ') #字母长度必须相等;第三个为可选参数,指定删除哪些字母
print(hello.translate(po))
#使用多个方法后,hello并没有改变,可以证明字符串是不可改变的。
print('原始字符串不变:',hello)

 

@ 字符串格式

对字符串调用format方法:

x = "I have {} apple {point} she is a {}."
print(x.format(5,'girl',point=','))
打印结果: I have 5 apple , she is a girl.

{ }命名的字段:将根据顺序 >>> girl 依次填充.

{ point }已命名字段:不用考虑顺序.

将索引作为字段名:按照索引顺序.

x = "This is {1} apple,she is a {0}."
print(x.format('girl',5))
打印结果:
This
is 5 apple,she is a girl.

 另一种方式:

name = 'world'
print("hello,%s"%name)
print("hello,{}".format(name))
打印结果: hello,world hello,world

 

 end~   ****** 几米花的Python ****** 博客主页:https://www.cnblogs.com/jimmy-share/  欢迎转载 ~,

·字符串(string)

笔记:

Python中用引号括起来的都是字符串,可以是单引号也可以是双引号

 This is a string

 This is a string

 

@ 字符串的基本操作

在序列中所有标准操作(索引、乘法、成员资格、长度、最小值、最大值)都适用字符串,然而,字符串是不可变的,所以赋值是非法的

h = 'hello,world!'
h[0] = 'p'
print(h)
打印结果: Traceback (most recent call last): File "test1.py", line 3, in <module>
    h[0] = 'p'
TypeError: 'str' object does not support item assignment #字符串不支持赋值

 

@字符串的方法

 字符串方法集合:

hello = '    this is my favorite    '
print(hello)
print('title()将字符串首字母替换成大写字母:',hello.title()) 
print('upper()将整体字符串字母替换成大写字母:',hello.upper()) 
print('lower()将整体字符串字母替换成小写字母:',hello.lower()) 
print('lstrip()删除字符串前的空白:',hello.lstrip())
print('rstrip()删除字符串后的空白:',hello.rstrip())
print('strip()删除字符串前、后的空白:',hello.strip())
print('find()查找子串的索引:',hello.find('t'))       #空格也有索引
print('center()在字符串两边填充字符(默认空格)并居中:',hello.center(31,'*'))
#split()括号中没有指定分割符,将默认在单个或多个空白字符(空格、制表符、换行符等)处进行拆分
x = hello.split()
print('split()将字符串装换为列表',x) 
fo = '+' #指定分割符
y = fo.join(x)
print('join()将列表转换为字符',y)  
print(y)
print('replace()替换指定字符串:',y.replace('+','-'))
po = str.maketrans('timf','TIMF',' ') #字母长度必须相等;第三个为可选参数,指定删除哪些字母
print(hello.translate(po))
#使用多个方法后,hello并没有改变,可以证明字符串是不可改变的。
print('原始字符串不变:',hello)

 

@ 字符串格式

对字符串调用format方法:

x = "I have {} apple {point} she is a {}."
print(x.format(5,'girl',point=','))
打印结果: I have 5 apple , she is a girl.

{ }命名的字段:将根据顺序 >>> girl 依次填充.

{ point }已命名字段:不用考虑顺序.

将索引作为字段名:按照索引顺序.

x = "This is {1} apple,she is a {0}."
print(x.format('girl',5))
打印结果:
This
is 5 apple,she is a girl.

 另一种方式:

name = 'world'
print("hello,%s"%name)
print("hello,{}".format(name))
打印结果: hello,world hello,world

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄