Hzangyao

导航

python基础之str的内置方法

str的内置方法

1把字符串转为全大写

string = 'jaSON123ly'
print(string.upper()) # JASON123LY

2把字符串转为全小写

print(string.lower()) # jason123ly

3判断以指定字符开头和结尾
print(string.startswith('ja'))
print(string.startswith('jaS'))

4 是否以某字符结尾
print(string.endswith('y'))
print(string.endswith('ly'))
print(string.endswith('123lyss'))

 

format的使用
第一种玩法:
# s = 'my name is {}, my age is {}'
# print(s.format('ly', 18))

# 第二种玩法:
# s = '{0} my name is {1}, my age is {0}, {1}, {1}, {0}, {2}'
# print(s.format('ly', 18, 'male', ))

# 第三种玩法:
s = ' my name is {name}, my age is {age}, {name}, {name}, {age}'
print(s.format(name='ly', age=18))

# 使用逗号分割一下列表数据,'ly,jason,tom,kevin'
name_list = ['ly', 'jason', 'tom', 'kevin']
s1 = name_list[0]
s2 = name_list[1]
s3 = name_list[2]
s4 = name_list[3]

print(s1 + ',' + s2 + ',' + s3 + ',' + s4 )
print(','.join(name_list))

# 替换字符串 replace
# s = 'my name is ly ly ly ly'
#
# print(s.replace('ly', 'jason', 1))

# 面试题:如何交互两个变量?m = 'hello',n='world',如何通过replace替换两个值呢

用isdigit()判断字符串是否全是整数,用布尔值返回

num = '12345abc'
print(num.isdigit())

 

posted on 2021-12-29 15:25  Hzangyao  阅读(52)  评论(0编辑  收藏  举报