string = "heLLo hOw are you toDay"
results = string.find("[A-Z]") <----- Here is my problem
string.lower().translate(table) <--- Just for the example.
>>>string
"olleh woh era uoy yadot"
#here i need to make the characters that where uppercase, be uppercase again at the same index number.
>>>string
"olLEh wOh era uoy yaDot"
我需要在上面的字符串中找到大写字符的索引号,并获取带有索引号的列表(或其他)以便再次使用字符串,以便在同一个字符串中恢复大写字符索引号。
也许我可以解决它的re模块,但我没有找到任何选项给我回索引号。 希望这是可以理解的,我做了一个研究,但无法找到解决方案。 感谢。
BTW,我正在使用python 3.X
答案 0 :(得分:2)
你可以沿着这条线做点什么,只需要稍微修改它并将这些开始位置收集到一个数组中等等:
import re
s = "heLLo hOw are you toDay"
pattern = re.compile("[A-Z]")
start = -1
while True:
m = pattern.search(s, start + 1)
if m == None:
break
start = m.start()
print(start)
答案 1 :(得分:1)
string = "heLLo hOw are you toDay"
capitals = set()
for index, char in enumerate(string):
if char == char.upper():
capitals.add(index)
string = "olleh woh era uoy yadot"
new_string = list(string)
for index, char in enumerate(string):
if index in capitals:
new_string[index] = char.upper()
string = "".join(new_string)
print "heLLo hOw are you toDay"
print string
显示:
heLLo hOw are you toDay
olLEh wOh era uoy yaDot