python基础语法--字符串魔法(1)

is alpha--判断字符串是否为字母,汉字,返回值布尔值

# 1.test = "alexalex "

# v1 = test.isalpha()

# print(v1)

# 字符串中有数字,特殊符号,空格键都会false

# is al num同理

# 2.test = "123 "

# # v1=test.isalnum()

# # print(v1)# 

format format_map将字符串中指定的占位符替换为指定的值,返回值为字符串

# test = "my name is {a} age {b}"

# v1 = test.format(a="孙家",b=24)

# v2 =test.format_map({"a":"孙家","b":24})

# print(v2)

 

# 数字判断--isdecimal digit numeric--判断字符串中是否为数字,返回值为布尔值

test = "#" 

# v1 = test.isdecimal()---支持"123"

# v2 = test.isdigit()---支持"123"

# # v3 = test.isnumeric()---支持"一""123""①⑩"

# print(v1,v2)

 

# is printable --用于判断字符串中是否存在不可打印的符号 例如制表符 \t  换行符 \n,返回值为布尔值

# test = "sahksc"

# v1 = test.isprintable()

# print(v1)

 

# is space --判断字符串中是否全部为空格,返回值为布尔值

# test = "  "

# v1 = test.isspace()

# print(v1)

 

# is title-- 判断标题和修改标题

# test = "Return True if all casedcharacters in S are uppercase and there is"

# v1 = test.istitle() --判断字符串是否为标题(所有单词首字母大写),返回值为布尔值

# v2 = test.title()   --将字符串转化为标题的形式,返回值为字符串

# print(v1,v2)

 

# join --以特定的符号分隔字符串,返回值为字符串

# test = "alex"

# q = "  "

# v1 = q.join(test)

# print(v1)

# 输出结果:a  l e  x

 

# upper lower --转换大小写,返回值为字符串

# test = "ALEx"

# v1 = test.isupper()  --判断字符串中是否全部为大写

# v2 = test.upper()    --将字符串全部转换为大写

# v3 = test.islower()  --判断字符串中是否全部为小写

# v4 = test.lower()    --将字符串全部转换为小写

# print(v1,v2,v3,v4)

# 输出结果:False ALEX Falsealex

 

# strip --去除字符串左右两边缩进

# 1.test = "exalex"

# v1 = test.strip("xe")  --左右两侧同时移除对应字符

# v2 = test.strip("ex")  --左右两侧同时移除对应字符

# v3 = test.lstrip("ex") --左侧移除

# v4 = test.rstrip("ex") --右侧移除

# v5 = test.strip("la")  --两侧移除,有限最多匹配

# v6 = test.strip("el")  --两侧移除,有限最多匹配

# print(v1,v2,v3,v4,v5,v6)

# 输出结果:al al alex exalexalex xalex

# 2.test = "  alex "  --去除左右所有空格

# v1 = test.strip()

# v2 = test.lstrip()

# v3 = test.rstrip()

# print(v1,v2,v3)

# 输出结果:alex alex     alex

# 3.test = "\n alex \t"--去除\t \n 对应的空格

# v1 = test.strip()

# print(v1)

# 输出结果:alex

 

# maketran translate --对应替换关系

# test ="asdsadasdasdasdasdasdsaasdsa"

# m =str.maketrans("asd","456")

# v = test.translate(m)

# print(v)

# 输出结果:4565464564564564564565445654

 

 

# 分割系列: --partition split 系列

# test = "asdsadsasdsasd"

# v1 =test.partition("d")   --从左侧开始,分割成三部分,且保留d

# v2 =test.rpartition("d")  --从右侧开始,分割成三部分,且保留d

# v3 = test.split("d",2)     --

# print(v1,v2,v3)

# 输出结果:('as', 'd','sadsasdsasd') ('asdsadsasdsas', 'd', '') ['as', 'sa', 'sasdsasd']

#

# test ="asdsad\nsdjflksdj\ndkjflk"

# v = test.splitlines(True)

# print(v)

# 输出结果:['asdsad\n','sdjflksdj\n', 'dkjflk']

 

 

# startswith endswith --以XX开头,以XX结尾

# test = "bend if you"

# v1 = test.startswith("a")---判断性语句

# v2 = test.endswith("u")   ---判断行语句输出值为布尔值

# print(v1,v2)

# 输出结果:False False

 

 

# swapcase ---大小写互换

# test = "aLex"

# v1 = test.swapcase()

# print(v1)

# 输出结果:AlEX

 

# is identifier --字母、数字、下划线:标识符 返回值布尔值

# test = "asd{name}"

# v1 = test.isidentifier()

# print(v1)

 

# replace --字符串之间的替换

# test = "alexalexalex"

# v1 =test.replace("ex","yy")

# v2 =test.replace("ex","yy",2)

# print(v1,v2)

# 输出结果:alyyalyyalyyalyyalyyalex

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值