Python--字符串基本操作

本文深入讲解了Python中字符串的各种操作方法,包括大小写转换、格式化、查找与替换、编码与解码等实用技巧,适合Python初学者及开发者快速掌握字符串处理技能。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Huanglinsheng

# name.capitalize()  首字母大写
# name.casefold()   大写全部变小写
# name.center(50,"-")  输出 '---------------------Alex Li----------------------'
# name.count('lex') 统计 lex出现次数
# name.encode()  将字符串编码成bytes格式
# name.endswith("Li")  判断字符串是否以 Li结尾"Alex\tLi".expandtabs(10) 输出'Alex      Li', 将\t转换成多长的空格
# name.find('A')  查找A,找到返回其索引, 找不到返回-1

'''format'''
'''
msg = "my name is {},my age is {}"
msg.format("hls",22)
print(msg.format("hls",22))

msg = "my name is {1},my age is {0}"
msg.format("hls",22)
print(msg.format("hls",22))

msg = "my name is {name},my age is {age}"
msg.format(name="hls",age=22)
print(msg.format(name="hls",age=22))


msg = "my name is {},my age is {}"
msg.format_map({'name':'alex','age':22})
'''

# msg.index('a')  返回a所在字符串的索引
# '9aA'.isalnum()   True
#
# '9'.isdigit() 是否整数
# name.isnumeric
# name.isprintable
# name.isspace
# name.istitle
# name.isupper
#  "|".join(['alex','jack','rain'])
# 'alex|jack|rain'


#字符替换
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab,outtab)
str = "this is string example....wow!!!"
str.translate(trantab)
print(str.translate(trantab))


#msg.swapcase 大小写互换

 

转载于:https://www.cnblogs.com/huanglinsheng/p/9361253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值