python 3 ---字符串方法使用整理

一、编码部分(结合执行结果进行理解)

name = "my \tname is {name} and i am {year} old"
print(name.capitalize())

首字母大写

print(name.center(50,"-"))
#打印50个字符,不够的用-补齐,并将chenhl放在中间。
print(name.encode())
#将字符串转成二进制,在python中0开头表示8进制,0x或者0X表示16进制,0b或者0B表示二进制。
print(name.endswith("hl"))
#判断字符串以什么结尾
print(name.expandtabs(tabsize=10))
#自动在字符串中的tab键处补上空格
print(name.format(name='chenhl',year='30'))
print(name.format_map({'name':'chenhl','year':'20'}))
#格式化输入,map 用于字典中
print(name.find("n"))
#获取n的索引,即位置,字符串可以切片
print(name.isalnum())
#判断变量是否是阿拉伯数字+阿拉伯字符,包含英文字符和0---9数字。是返回true,非则相反。
print('name'.isalpha())
#判断变量是否是纯英文字符,是返回true,非则相反。
print(name.isdecimal())
print('1.23'.isdecimal())
print('11'.isdecimal())
print('1B'.isdecimal())
#判断是否是十进制
print(name.isdigit())
#判断是不是整数
print('name'.isidentifier())
#判断是不是一个合法的标识符
print(name.islower())
#判断是不是小写
print(name.isnumeric())
print('12'.isnumeric())
print('1.2'.isnumeric())
#判断是不是只有数字
print('my name is'.istitle())
print('My Name Is'.istitle())
#判断是不是首字母大写
print('my name is '.isprintable())
#判断是不是可以打印的文件,tty的文件会返回非。
print('my future'.isupper())
#判断是不是都是大写
print(','.join(['1','2','3']))
#将列表的内容转成字符串,或将join内容当成一条指令交给os执行。
print(name.ljust(50,''))
#保证字符串的长度,不够用
在右侧补齐。
print(name.rjust(50,'-'))
#保证字符串长度,不够用-在左侧补齐。
print('ABCD'.lower())
#把大写变成小写
print('abcd'.upper())
#把小写变成大写
print(' abcd'.lstrip())
print('\nabcd'.lstrip())
#去掉左侧的空格或回车。rstrip,去掉右侧的空格或回车,strip去掉两侧的。
p = str.maketrans('abcdef','123456')
print('chenhl'.translate(p))
#将字符串换成后面数字对应的值
print('chenhl'.replace('h','H',1))
#将h替换成大写H,count替换几个
print('chenhl'.rfind('h'))
#找到值得最后一个位置
print('ch en hl'.split())
print('ch en hl'.split('h'))
#把字符串按照空格或固定字符转成列表
print('che\nhl'.splitlines())
#把字符串按照换行转成列表,特别是不同系统的换行
print('chenhl'.startswith('c'))
#判断以什么开头的字符串
print('Hello World'.swapcase())
#将大写的首字母转换成小写
print('hello world'.title())
#将首字母变成大写
print('hello world'.zfill(20))
#用零补位,

二、执行结果
My name is {name} and i am {year} old
------my name is {name} and i am {year} old------
b'my \tname is {name} and i am {year} old'
False
my name is {name} and i am {year} old
my name is chenhl and i am 30 old
my name is chenhl and i am 20 old
4
False
True
False
False
True
False
False
True
True
False
True
False
False
True
True
False
1,2,3
my name is {name} and i am {year} old****
------------my name is {name} and i am {year} old
abcd
ABCD
abcd
abcd
3h5nhl
cHenhl
4
['ch', 'en', 'hl']
['c', ' en ', 'l']
['che', 'hl']
True
hELLO wORLD
Hello World
000000000hello world

转载于:https://blog.51cto.com/metis/2051065

### Python 中删除字符串中的指定字符 在 Python 中,可以通过多种方式实现删除字符串中的指定字符。以下是基于已有引用内容以及专业知识整理的相关解决方案。 #### 方法一:使用列表推导式过滤不需要的字符 通过 `list` 和列表推导式可以轻松移除目标字符并重新组合成新的字符串: ```python def remove_chars(s, chars_to_remove): result = ''.join([char for char in s if char not in chars_to_remove]) return result s = "HelloWorld!" chars_to_remove = "lo" new_s = remove_chars(s, chars_to_remove) print(new_s) # 输出: HeWrd! ``` 此方法的核心在于利用列表推导式筛选掉不希望保留的字符[^1]。 --- #### 方法二:使用内置函数 `str.translate()` 和 `str.maketrans()` Python 提供了一个高效的方式——`translate()` 函数配合 `maketrans()` 使用来完成批量替换或删除操作: ```python import string s = "HelloWorld!" remove_set = set("lo") # 创建翻译表,其中要删除的字符映射为 None translation_table = str.maketrans('', '', ''.join(remove_set)) result = s.translate(translation_table) print(result) # 输出: HeWrd! ``` 这种方法适用于需要处理大量数据的情况,性能优于简单的循环遍历[^3]。 --- #### 方法三:正则表达式替代法 如果涉及复杂的模式匹配或者动态调整需求,则可借助模块 `re` 实现更灵活的功能: ```python import re s = "HelloWorld!" pattern = '[lo]' # 定义待删字符集合 result = re.sub(pattern, '', s) print(result) # 输出: HeWrd! ``` 上述代码片段展示了如何定义一个正则表达式的模式去匹配所有需被清除的目标字符,并将其替换成空白[^4]。 --- ### 总结 以上三种方案各有优劣,在实际开发过程中可根据具体场景选择最合适的工具和技术手段解决问题。对于简单任务推荐采用第一种直观易懂的办法;而对于追求效率的大规模运算建议尝试第二种技术路线;最后当面临复杂条件约束时考虑引入第三类高级特性支持下的解决策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值