python去除换行符和空格

str1 = '2222222222222222asdasdadas22222222222'
# 去除首位字符串
print(str1.strip('2'))
str2 = '              asdasdadas             '
# 去除首位字符串空格
print(str2.strip())
# replace() 替换中间字符串
#str.replace(old, new[, max])
# old – 将被替换的子字符串。
# new – 新字符串,用于替换old子字符串。
# max – 可选字符串, 替换不超过 max 次
print(str1.replace('a','C'))
# 去除首位字符串空格,中间空格,回车符,换行符
print(str.strip().replace(' ', '').replace('\n', '').replace('\r', ''))

posted @ 2022-05-13 14:17  darling331  阅读(456)  评论(0编辑  收藏  举报