欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > Python字符串格式

Python字符串格式

2024/10/25 16:24:38 来源:https://blog.csdn.net/weixin_46287157/article/details/141786239  浏览:    关键词:Python字符串格式

文章目录

  • 1. 数字与ASCII码转换
  • 2. 字符串输出格式(%)
    • 2.1 数字进制与小数表示
    • 2.2 字符串长度与对齐方式
  • 3. 字符串输出格式(f'')
  • 4. 字符串输出格式(format)
  • 5. 转义字符(换行、Tab)
  • 6. 字符串查找统计替换等

1. 数字与ASCII码转换

将ASCII码转化为数字或者将数字转化为ASCII码时,可以使用ord()chr()方法实现,代码如下:

# ASCII码转数字
print(ord("A"))        # 65
print(type(ord("A")))  #  <class 'int'>
# 数字转化为ASCII码
print(chr(65))         # A

除上面方法之外,将数字转化为ASCII码,还可以使用%c格式,代码如下:

# 数字转字符
a = "%c" % 65
print(a, type(a))          # A <class 'str'>
print("%c" % (65 + 32))    # a
print("%c %c" % (65, 66))  # A B
字符含义
%c单个字符,或将数字转化为字符
%s字符串

2. 字符串输出格式(%)

该部分输出格式都是使用%的格式表示输出,代码如下:

print("%c" % 65)
print("%.5f" % (2/3))

2.1 数字进制与小数表示

字符含义
%d有符号十进制
%u无符号十进制
%o八进制
%x十六进制(小写)
%X十六进制(大写)
%f浮点数
%e科学计数法

代码示例:

print("%d" % 123)
print("%o %x %X" % (342, 342, 342))
print("%f" % (2/3))
print("%.5f" % (2/3))    # 浮点类型保留5位小数'''
123
526 156 156
0.666667
0.66667
'''

2.2 字符串长度与对齐方式

print("%d" % 123)
print("%5s" % "abc")   # 保留5位长度,不够则前面补空格
print("%-5s" % "abc")  # 保留5位长度,不够则后面补空格
print("%7.5d" % 123)   # 先看长度是否够5位,不够则前面补0,补完后再看长度是否够7位,不够则前面补空格
'''
123abc
abc  00123
'''

3. 字符串输出格式(f’')

在一些以前的python版本不支持f''格式

name = 'Tom'
age = 11
print(f'name: {name} age: {age} score : {99}')  # name: Tom age: 11 score: 99
print(f'=={name}==')       # ==Tom==
print(f'=={name:20}==')    # ==Tom                 ==
print(f'=={name:->20}==')  # ==-----------------Tom==
print(f'=={name:-<20}==')  # ==abc-----------------==

4. 字符串输出格式(format)

字符含义
b二进制
x十六进制
f小数
%百分号格式

出去上面的,整数使用的有bcdxXn,浮点数有eEfFgGn%

print("{},{}".format(1,2))  # 按顺序输出结果
# 1,2
print("{1},{0}".format(1,2))  # 按索引位置输出结果
# 2,1
print("{0:10}".format('afweg'))  # 字符串不够十位,后面空格填充
# afweg
print("{0:->10}".format('afweg'))  # 字符串不够十位,前面用-补充
# -----afweg
print("{0:-<10}".format('afweg'))  # 字符串不够十位,后面用-补充
# afweg-----
print("{0:.2f}".format(1.234))   # 保留两位小数
# 1.23
print("{:b} {:x} {:%}".format(24, 24, 24))
# 11000 18 2400.000000%

5. 转义字符(换行、Tab)

字符含义
\n换行
\tTab
print('a\nc')
'''
a
c
'''
print('a\nc\td')
'''
a
c   d
'''

6. 字符串查找统计替换等

s为字符串含义说明
字符串查找find找不到返回-1,index找不到则报错,用法和find一样
s.find(‘o’)返回第一个位置
s.index(‘world’)返回第一个位置
s.find(‘world’, 6)从第6个位置开始找
s.find(‘world’, 6, 20)从第6找到第20
s.index(‘world’, 6, 20)从第6找到第20
rfind rindex都是从后往前找,用法同上
s.rfind(‘o’)后往前第一个
s.rindex(‘o’)后往前第一个
字符串替换
s.replace(‘world’, ‘aaa’)替换匹配的第一个,替换后原字符串不变
s.replace(‘o’, ‘a’, 2)替换两个o
字符串统计
s.count(‘h’)多少个h
按字符分隔s = 'hello world and hello word'
s.split()[‘hello’, ‘world’, ‘and’, ‘hello’, ‘word’]
s.split(’ ')以空格分隔 同上
s.split(’ ', 2)分隔两次[‘hello’, ‘world’, ‘and hello word’]
s = 'hello\tworld and\t\n hello\n word'
s.splitlines()按行分隔[‘hello\tworld and\t’, ’ hello’, ’ word’]
s = 'hellohahaHahellohellohahaHahello'
s.partition(‘ha’)按分隔条件将字符串分隔三部分,分隔条件前,分隔条件,分隔条件后(‘hello’, ‘ha’, ‘haHahellohellohahaHahello’)
s.rpartition(‘ha’)从后往前(‘hellohahaHahellohelloha’, ‘ha’, ‘Hahello’)
字符串合并
s = 'hello world aaa'
‘-’.join(s)# h-e-l-l-o- -w-o-r-l-d- -a-a-a
ss=['hello', 'world', 'aaa']
‘-’.join(ss)hello-world-aaa
‘’.join(ss)helloworldaaa
字符串判断
‘123456’.startswith(‘123’)是否以123开头
‘123456’.endswith(‘123’)是否以123结尾
‘hel’.isupper()是否都为大写
‘hwl’.islower()是否都为小写
‘123abc’.isalpha()是否都为字母
‘1223gew’.isalnum()是否都为数字或字母
‘1234’.isdigit()是否都为数字
‘\n’.isspace()是否是空白字符,包括空格,换行符\n,制表符\t
字符转大小写
a.upper()全转大写
a.lower()全转小写
print(a.title()将每个单词首字符转大写
print(a.capitalize()将地体格单词首字母转大写
字符串对齐
a.center(11)按给定宽度居中对其
a.center(11,‘_’)空白部分用_填充
a.rjust(11)左对齐
a.ljust(11)右对齐
去除空格
a.strip()去除两边空白
a.lstrip()去除左侧空白
a.rstrip()去除右侧空白

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com