简体   繁体   中英

How to add a space before the first letter of continuous uppercase letters in Python?

How to add a space before the first letter of continuous uppercase letters in Python?

For example, if a string is "ABCDEzyxFGwvuHts", it should be split as: " ABCDExyz FGwvu Hts"

Thanks.

Use the following simple solution with re.sub() function:

import re

s = "ABCDEzyxFGwvuHts"
result = re.sub(r'([A-Z]+[^A-Z]+)', r' \1', s)

print result

The output:

 ABCDEzyx FGwvu Hts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM