[英]find index number of uppercase character in a string
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模块来解决它,但是我没有找到任何可以返回索引号的选项。 希望这是可以理解的,我做了一个研究,但无法找到解决方案。 谢谢。
顺便说一下,我正在使用python 3.X
您可以沿着这条线做一些事情,只需要稍微修改一下并将这些起始位置收集到一个数组中,等等:
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)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.