Home > Software design >  how to extract a sentence from string in python using simple for loop?
how to extract a sentence from string in python using simple for loop?

Time:10-11

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 = "")

CodePudding user response:

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] != " ":

CodePudding user response:

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.

CodePudding user response:

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] != " ":
   ...
  • Related