Python中字符串的用法详解

2022-05-03 00:00:00 字符串 详解 用法
"""
皮蛋编程(https://www.pidancode.com)
创建日期:2022/4/25
功能描述:Python中字符串的用法详解
"""
# 字符串可以看作字符列表
s = 'Howdy'
print(s)
print(len(s))
print(s[3])
print(s[1:3])
t = ' dude! '
s += t
print(s + '|')
print(s.strip() + '|')
s = s.rstrip('! ')
print(s)

s = 'Howdy dude!'
print(s.lower())
print(s.upper()[:5])
print(s.title())
print(s.replace('Howdy', 'Greetings'))
print(s)
print(s.count('d'))
print(s.find('w'))
print('dud' in s)
print('X' not in s)
print(s.startswith('How'))
print(s.endswith('cat'))
print(s > 'Honk')
print(s.isalpha())
print(s[0:4].isalpha())
print(s.isnumeric())

print(s.split())
print('5,7,9'.split(','))
print('73.294'.split('.'))

print(s[0], '\t', s[1], '\t', s[2])
print(s[:s.find(' ')] + '\n' + s[s.find(' ')+1:])

输出:

Howdy
5
d
ow
Howdy dude! |
Howdy dude!|
Howdy dude
howdy dude!
HOWDY
Howdy Dude!
Greetings dude!
Howdy dude!
3
2
True
True
True
False
True
False
True
False
['Howdy', 'dude!']
['5', '7', '9']
['73', '294']
H    o   w
Howdy
dude!

相关文章