python字符串
python专栏地址
上一篇: python-基础(2)-数值运算
下一篇:python-基础(4)-list
本节学习python字符串的基本用法
- 字符串初识
- 字符串基本操作函数
- 字符串高级操作
一、字符串初始
- 定义字符串
- 字符串的+ 与 *(加与乘)
- 字符串长度求解函数len(str)
1.1 定义
str='Hello python'
print(str)
Hello python
1.2 + 与*
- + 就是将两个字符串进行拼接
- *一个数n,就是将一个字符串复制n次
- len(str)得到一个字符串的长度
gufen ='Hello'+'python'+'将前面两个进行拼接了'
gufen
'Hellopython将前面两个进行拼接了'
gufen*3
'Hellopython将前面两个进行拼接了Hellopython将前面两个进行拼接了Hellopython将前面两个进行拼接了'
1.3 len 长度
len(gufen) # //求长度
21
二、字符串操作
- 切分
- s.split(‘,’):切分函数,’ '中间可以为空,即什么都不写,表示用空格作分隔符,也可以别的符号
- s.join(s2):合并,s2是一个数组,这个合并更带插入的意思,在s2的间隔中插入s
- s,replace(s1):替换,自己本身并不发生改变
- s.upper()大写化
- s.lower()小写化
- s.strip()去掉括号
- 去掉左空格:lstrip()
- 去掉右空格:rstrip()
2.1 切分
gufen='1 2 3 4 5'
s1=gufen.split()
print(s1)
print(s1[0])
print(s1[2:5])
['1', '2', '3', '4', '5']
1
['3', '4', '5']
用split()函数切分,函数里面可以加东西
gufen='1,2 3 4 5'
gufen0=gufen.split(',')
print(gufen0)
print(gufen0[0])
print(gufen0[1])
['1', '2 3 4 5']
1
2 3 4 5
2.2 合并
join是字符串的函数,因此只能通过字符串来调用,join()里面可以是list,也可以是字符串
# 列表,list有n部分,将会插入(n-1)个的gufen3gufen1=['1','2','3']
gufen2='||'
gufen3='56'
print("测试1:", gufen2.join(gufen1))
print("测试2:", gufen3.join(gufen1))
测试1: 1||2||3
测试2: 1562563
gufen1
['1', '2', '3']
# 把列表用逗号隔开,需要注意的是, 列表的成员必须是字符串
gufen3=','
gufen3.join(gufen1)
'1,2,3'
2.3 替换
replace()函数
gufen='Hello python'
gufen.replace('python','world')
'Hello world'
# 本身并没有改变
gufen
'Hello python'
gufen='Hello python'
gufen2=gufen.replace('python','world')
gufen2
'Hello world'
2.4 大小写转换
- upper()函数
- lower()函数
gufen='Hello python'
gufen.upper()
'HELLO PYTHON'
gufen.lower()
'hello python'
2.5 去掉空格
左右都去掉:strip()
去掉左空格:lstrip()
去掉右空格:rstrip()
gufen=' hello world '
gufen.lstrip()
'hello world '
gufen.strip()
'hello world'
gufen.rstrip()
' hello world'
三、高级操作
3.1 基本用法
在Python中,format()
是一个内置的字符串方法,用于格式化字符串。它允许你插入和格式化变量到字符串中。format()
方法可以与命名参数或位置参数一起使用,并提供了对格式化过程的精细控制。
- 无序传参
# 传参
'{} {} {} {}'.format('gu','feng','de', "world")
'gu feng de world'
- 有序传参
# 按照序号传参
'{0} {2} {1} {3}'.format('gu','feng','de', "world")
'gu de feng world'
# 按照赋值传参
'{world} {gu} {feng} {de}'.format(gu=10,feng=5,de=1, world=4)
'4 10 5 1'
#按照格式传参
gufen='gu feng de world:'
b=456.0
c=789
result='%s %f %d' % (gufen,b,c)
result
'gu feng de world: 456.000000 789'
3.2 格式化数字
template = "The price is {:.2f} dollars."
price = 123.456789
formatted_price = template.format(price)
print(formatted_price) # 输出: The price is 123.46 dollars.
The price is 123.46 dollars.
template = "You have {:,} points."
points = 1234567
formatted_points = template.format(points)
print(formatted_points) # 输出: You have 1,234,567 points.
You have 1,234,567 points.
2.3 格式化字符串宽度和对齐
# 指定宽度和对齐方式
template = "{:<10} | {:^10} | {:>10}"
data = ("apple", "banana", "cherry")
formatted_data = template.format(*data)
print(formatted_data)
apple | banana | cherry
3.4 格式化字符串宽度和对齐
template = "The {animal} jumps over the {obstacle}."
data = {'animal': 'frog', 'obstacle': 'log'}
message = template.format(**data)
print(message) # 输出: The frog jumps over the log.
The frog jumps over the log.
3.5 格式化日期和时间
from datetime import datetime# 格式化日期和时间
now = datetime.now()
template = "The current date and time is {:%Y-%m-%d %H:%M:%S}."
formatted_datetime = template.format(now)
print(formatted_datetime) # 输出当前日期和时间
The current date and time is 2024-04-19 21:23:32.
python专栏地址
上一篇: python-基础(2)-数值运算
下一篇:python-基础(4)-list
点个关注呗 🤪😝