簡體   English   中英

查找字符串中的大寫字符的索引號

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM