Reputation: 11
Here is my code:
x = str(input())
w = ' '
w2 = ''
for i in x:
if i.isupper() == True:
w2 += w
else:
w2 += i
print(w2)
I have converted the uppercase alphabets to space, can anyone suggest me what to do next?
Input: abCdefGh
Expected output: def
Upvotes: 1
Views: 1227
Reputation: 3319
You can use regex
to extract text easily.
import re
# pattern to accept everything between the capital letters.
pattern = '[A-Z](.*?)[A-Z]'
# input string
input_string= 'thisiSmyString'
matched_string = re.findall(pattern, input_string)
print(matched_string[0])
Upvotes: 1
Reputation: 534
I use a flag to tag whether program should add character i
into w2
.
Notice the condition I write. It is the key of my code.
x = "abCdefGh"
w2 = ''
inBetween = False
for i in x:
if i.isupper():
# Upper case denotes either start or end of substring
if not inBetween:
# start of substring
inBetween = True
continue
else:
# end of substring
inBetween = False
break
if inBetween:
w2+= i
print(w2)
Result is :
def
Upvotes: 1
Reputation: 1369
To print the substring that is present between two uppercase letters in the string.
Step 1: Find the index position of the uppercase letter.
Step 2: Then slice the substring between the first element + 1 and the second element in the list (string[start: end])
word = "abCdefGh"
# Get indices of the capital letters in a string:
def getindices(word):
indices = []
for index, letter in enumerate(word):
if (letter.isupper() == True):
indices.append(index)
return indices
if __name__ == "__main__":
caps_indices = getindices(word)
start = caps_indices[0] + 1
end = caps_indices[1]
print(word[start:end])
Output:
def
Upvotes: 1