程序员之所以犯错误,不是因为他们不懂,而是因为他们自以为什么都懂。
# strip去除空格
s = ' abcd efg '
print(s.strip())
print(s.lstrip())
print(s.rstrip())
print(s)
结果:
abcd efg
abcd efg
abcd efg
abcd efg注意空格
# 字符串连接
print('abc_' + 'defg')
s = 'abcdefg'
s += '\nhijk'
print(str)
结果 :
abc_defg
<class 'str'>
# 大写小
s = 'abc defg'
print(s.upper())
print(s.upper().lower())
print(s.capitalize())
结果 :
ABC DEFG
abc defg
Abc defg
# 位置和比较
s_1 = 'abcdefg'
s_2 = 'abdefgh'
print(s_1.index('bcd'))
try:
print(s_1.index('bce'))
except ValueError:
print('ValueError: substring not found')
print(s_1 == s_1) # cmp函数被Python3移除了
print(s_1 > s_2)
print(s_2 > s_1)
结果 :
1
ValueError: substring not found
True
False
True
# 分割和连接
s = 'abc,def,ghi'
print(s.split(','))
s = '123\n456\n789'
numbers = s.splitlines()
print(numbers)
print('-'.join(numbers))
结果 :
['abc', 'def', 'ghi']
['123', '456', '789']
123-456-789
# 常用判断
s = 'abcdefg'
print(s.startswith('abc'))
print(s.endswith('efg'))
print('abcd1234'.isalnum())
print('\tabcd1234'.isalnum())
print('abcd'.isalpha())
print('12345'.isdigit())
print(' '.isspace())
print('acb125'.islower())
print('A1B2C'.isupper())
print('Hello world!'.istitle())
结果 :
True
True
True
False
True
True
True
True
True
False
# 数字到字符串
print(str(5))
print(str(5.))
print(str(-5.23))
print(int('1234'))
print(float('-23.456'))
# 格式化字符串
print('Hello %s!' % 'world')
print('%d-%.2f-%s' % (4, -2.3, 'hello'))
结果 :
5
5.0
-5.23
1234
-23.456
Hello world!
4--2.30-hello