0

str1 = "srbGIE JLWokvQeR DPhyItWhYolnz"

Like I want to extract I Love Python from this string. But I am not getting how to. I tried to loop in str1 but not successful.

i = str1 .index("I")

for letter in range(i, len(mystery11)):
  if letter != " ":
    letter = letter+2
  else:
    letter = letter+3
  print(mystery11[letter], end = "")
Code With Faraz
  • 62
  • 2
  • 12

5 Answers5

0

In your for loop letter is an integer. In the the first line of the loop you need to compare mystery[11] with " ":

if mystery11[letter] != " ":
PiedPiper
  • 5,735
  • 1
  • 30
  • 40
0

You can use a dict here, and have char->freq mapping of the sentence in it and create a hash table. After that you can simply iterate over the string and check if the character is present in the hash or not, and if it is present then check if its count is greater than 1 or not.

Neel Patel
  • 11
  • 3
0

Don't know if this will solve all your problems, but you're running your loop over the indices of the string, This means that your variable letter is an integer not a char. Then, letter != " " is always true. To select the current letter you need to do string[letter]. For example,

if mystery11[letter] != " ":
   ...
alexmolas
  • 110
  • 1
  • 10
0

Here's how I'd go about:

  1. Understand the pattern of the input: words are separated by blank spaces and we should get every other letter after the first uppercase one.
  2. Convert string into a list;
  3. Find the first uppercase letter of each element and add one so we are indexing the next one;
  4. Get every other char from each word;
  5. Join the list back into a string;
  6. Print :D

Here's the code:

def first_uppercase(str):
    for i in range(0, len(str)): 
        if word[i].istitle(): 
            return i
    return -1 

def decode_every_other(str, i):
    return word[i::2]

str1 = "srbGIE JLWokvQeR DPhyItWhYolnz"

# 1 
sentence = str1.split()
clean_sentence = []
for word in sentence:
    # 2
    start = first_uppercase(word) + 1
    # 3
    clean_sentence.append(decode_every_other(word, start))

# 4
clean_sentence = ' '.join(clean_sentence)

print("Input: " + str1)
print("Output: " + clean_sentence)

This is what I ended up with:

Input: srbGIE JLWokvQeR DPhyItWhYolnz
Output: I Love Python

I've added some links to the steps so you can read more if you want to.

Dharman
  • 30,962
  • 25
  • 85
  • 135
acrlnb
  • 88
  • 7
0
def split(word):
    return [char for char in word]

a = input("Enter the original string to match:- ")
b = input("Enter the string to lookup for:- ")

c = split(a)
d = split(b)
e = []

for i in c:
    if i in d:
        e.append(i)

if e == c:
    final_string = "".join(e)
    print("Congrats!! It's there and here it is:- ", final_string)
else:
    print("Sorry, the string is not present there!!")
AMN
  • 1
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 11 '21 at 15:32