python 字符串与格式化输出

字符串的定义

字符串是一种由字符组成的有序序列,通常用于表示文本或文本数
在Python中,用引号引起的都是字符串,其中引号可以是单引号,也可以是双引号

'holle word'
"holle word"

1.1字符串通用操作

相加:字符串+字符串  (进行拼接)
相乘:字符串*整数 (多次复制拼接)
获取字符串长度: len()
索引取值
切片
for 循环取值

1.2 字符串的修改

replace()

字符串名.repalce('要修改的值''替换值')
字符串名.repalce('要修改的值''替换值',替换的次数)
str = ""
res = str.replace('你','大家')
print(res)
# 默认全部修改
res1 =str.replace('你','大家',2)
print(res1)
#指定修改次数,从前往后修改
res2 =str.replace('的','')
print(res2)
# 把字符修改成空字符就是删除它
res3 = str.replace('go','他')
print(res3)
#指定的元素不存在不报错

注意事项:

1,想要对字符进行删除,就可以把字符替换成空字符

2,没有指定替换次数,默认是替换全部

3,替换次数可以随意设置,从前往后依次替换

4,指定的字符不存在,没有报错。

1.3 字符串的删除

split()

切割字符串,把字符串变成列表,被切割的字符串变成了空元素

字符串名.split('切割字符')
字符串名.split('切割字符',切割次数)
str1 = '大家好,我是最最最最最最最最最最最最好的朋友'

print(str1.split('我'))
# print(str1.split('我','的'))
# 只能指定一个字符进行切割.
print(str1.split('最',1))
print(str1.split('最',2))
print(str1.split('最',3))
print(str1.split('最',4))
# 切割次数跟逗号的数量是一样的
print(str1.split('大'))
print(str1.split('友'))
# 切割首位字符时一定会有空字符

注意点:

​ 1切割次数和逗号相等的

​ 2一次只能指定一个单独的字符

​ 3 首尾字符切割会出现空字符

strip()

删除字符串首尾空格,不删除中间的

lstrip()

删除左边的空格

rstrip()

删除右边的空格

str1 = '  hello  world  '

print(str1.strip())
print(str1.lstrip())
print(str1.rstrip())

如何删除中间的空格

replace()替换成空字符

print(str1.replace(' ',''))

upper()

把字符串中的英文字母全部大写

字符串.upper()

str1 = '  hello  world  '
str2 = '  Hello  World  '
str3 = ' 你好 Hello  World  '

print(str1.upper())
print(str2.upper())
print(str3.upper())

lower()

把字符串中的英文字母全部小写

str1 = '  hello  world  '
str2 = '  Hello  World  '
str3 = ' 你好 Hello  World  '

print(str1.lower())
print(str2.lower())
print(str3.lower())

title()

把字符串的引文单词/(连续的英文字母)首字母大写

str1 = 'helloworld'
str2 = 'HelloWorld'
str3 = '你好HelloWorld '

print(str1.title())
print(str2.title())
print(str3.title())

1.4字符串的查询

count()

统计某个元素(字符)出现的次数

str1 = 'helloworld'

print(str1.count('l'))
print(str1.count('h'))
print(str1.count('f'))

find()

查询字符对应的下标

字符串名.find('字符')
字符串名.find('字符',起点,终点)

print(str1.find('w'))
print(str1.find('l'))
# 如果出现重复的返回的是最小的下标
print(str1.find('f'))
print(str1.find('g'))
print(str1.find('p'))
# 指定的字符不存在返回-1

print(str1.find('l',5,10))
print(str1.find('l',0,2))
# 区间包头不包尾

find :find不存在返回的是-1

endswith()

判断字符串末尾的字符是否与我们指定的字符一致,返回bool值

字符串名.endswith('指定字符串')

str1 = 'hello world'
print(str1.endswith('he'))
print(str1.endswith('e'))
print(str1.endswith('d'))
print(str1.endswith('ld'))
print(str1.endswith('world'))
print(str1.endswith('hello world'))

startswith()

判断字符串开头的字符是否与我们指定的字符一致,返回结果是bool

str1 = 'hello world'

print(str1.endswith('he'))
print(str1.endswith('e'))
print(str1.endswith('d'))
print(str1.endswith('ld'))
print(str1.endswith('world'))
print(str1.endswith('hello world'))
print(str1.startswith('he'))
print(str1.startswith('hello'))
print(str1.startswith('world'))

isdigit()

判断字符串中是否为纯数字,返回结果也是bool

str1 = 'hello world'
str2 = '0001'
str3 = '请问你今年多少岁'
str4 = '我今年18'
str5 = '19.66'

print(str1.isdigit())
print(str2.isdigit())
print(str3.isdigit())
print(str4.isdigit())
print(str5.isdigit())
res = int(str2)
print(res)
print(type(res))

isalpha()

判断字符串是否都为(英文,中文)返回结果也是bool

str1 = '请问你今年多少岁'
str2 = 'helloworld'
str3 = 'hello123'
str4 = '我今年18'
str5 = '123'

print(str1.isalpha())
print(str2.isalpha())
print(str3.isalpha())
print(str4.isalpha())
print(str5.isalpha())
# 纯中文/纯英文

二、格式化输出

2.1 f格式化输出

大括号里面写变量,引号外面写f。大括号里面可以写变量,或者数字

name = 'luo'
age = 18
print('大家好,我是',name,'我今年',age,'岁')
print(f'大家好,我是{name},我今年{age}岁')
day = '20241011'
print(f'今天是{day[0:4]}{day[4:6]}{day[6:]}日')

2.2 %占位符格式化输出

#python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符
% 占位符
%s 字符串占位
%d 整型占位
%f 浮点型占位


name = 'luo'
age = 18
hight = 180

print('''
个人介绍
----------------------------
姓名:%s
年龄:%d
身高:%.2f
'''%(name,age,hight))

print('大家好我是%s'%name,'我的年龄是%d'%age,'我%.2f厘米'%hight)
#   ​%f   格式化浮点数字,可指定小数点后的精度
#   %s	 格式化字符串
#   %d	 格式化整数

2.3 format()格式化输出


name = 'luo'
age = 18
hight = 181.263514


print('''
------------------------
个人介绍
姓名:{}
年龄:{}
身高:{}
'''.format(name,age,hight))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值