【发布时间】:2020-09-14 07:17:17
【问题描述】:
我想只在 Python 中的右箭头之后(直到它碰到逗号)将字符串变为小写。
另外,如果可以的话,我更喜欢用一行来写。
这是我的代码:
import re
line = "For example, →settle ACCOUNTs. After that, UPPER CASEs are OK."
string1 = re.sub(r'→([A-Za-z ]+)', r'→\1', line)
# string1 = re.sub(r'→([A-Za-z ]+)', r'→\1.lower()', line) # Pseudo-code in my brain
print(string1)
# Expecting: "For example, →settle accounts. After that, UPPERCASEs are OK."
# Should I always write two lines of code, like this:
string2 = re.findall(r'→([A-Za-z ]+)', line)
print('→' + string2[0].lower())
# ... and add "For example, " and ". After that, UPPER CASEs are OK." ... later?
我相信一定有更好的方法。你们会怎么做呢?
提前谢谢你。
【问题讨论】:
标签: python substitution lowercase re