-3

For instance, if the user entered "Hello welcome to the code" and the program was looking for the character 'e' the program would print: Hello welcome the code

  • Does this answer your question? [String formatting in Python 3](https://stackoverflow.com/questions/13945749/string-formatting-in-python-3) – Bugbeeb Jul 09 '20 at 01:08
  • Welcome to SO. Please provide detailed information about your problem and [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example). Please, check [how-to-ask](https://stackoverflow.com/help/how-to-ask) a question for more details as well. – alan.elkin Jul 09 '20 at 01:31
  • Your problem is two parts: [How to split a string into a list?](https://stackoverflow.com/q/743806/2745495) and [Check if a Python list item contains a string inside another string](https://stackoverflow.com/q/4843158/2745495). Depending on how you parsed the string, you might also need [Concatenate item in list to strings](https://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings). – Gino Mempin Jul 09 '20 at 08:56

3 Answers3

1

This looks like a honework assignment ... if thats the case, please keep in mind that if u dont understand the basics and u dont try to understand it, you will never learn it ... also, SO is here to help u with your problem, when u get stuck by getting somekinda error or something ... right now all u did is nothing and u r just crying for help ... but I have a good more, so here is the code that does it (u should know how to ha dle input ;))

inp = "Bla bla bla bla tra tra tea"
inp2 = "r"
words = inp.split(" ")
for w in words:
    if inp2 in w:
        print(w)

Also u need to fix formatting (this eill print each word to a different line ;) ... if u have any other questions, please let me know :)

StyleZ
  • 1,276
  • 3
  • 11
  • 27
  • Your code will raise a `NameError`. (Hint: variable names are case-sensitive.) [EDIT: now corrected I see.] – alani Jul 09 '20 at 01:00
  • It is not a homework assignment lol, just trying to expand my knowledge by doing random tasks like this. But so I do understand, how does the inp.split work? – Connor Oflanagan Jul 09 '20 at 01:00
  • 1
    @ConnorOflanagan If you do `inp = "Bla bla bla bla tra tra tea"` and then `help(inp.split)`, it will tell you what it does. – alani Jul 09 '20 at 01:01
  • 1
    @alaniwi thx, i typed it on a phone, it should be fixed now ... also ->Connor u should specify what you have a problem with and show us your partial solution :) ... anyways gl in your journey if its true (but many ppl are comming here with similar assignment, just cuz they are lazy to do them) – StyleZ Jul 09 '20 at 01:04
0

It seems like you want to print all words that contain a specific letter. Just split the string on the spaces then either remove all the words that don't have the char or add those that do to a new list then join the list on a space to recombine it. This approach does assume there is are spaces between each word not tabs or newlines (you can just remove the ' ' from the split to split on whitespace but this approach always reassembles the good words using single spaces.

words = line.split(' ')
goodWords = []
for word in words:
    if word.find(keyChar) != -1:
        goodWords.append(word)
goodLine = ' '.join(goodWords)
David Oldford
  • 1,127
  • 4
  • 11
  • Could be written `' '.join(word for word in line.split() if keyChar in word)` if you want (and in your version, you can use `if keyChar in word:` rather than needing to use `find`). It is possible that the one-liner is not appropriate for the OP's level of python, but it is still worth showing use of `in` here, I think. – alani Jul 09 '20 at 00:53
0

Just use the .split function, it will split each individual word up into an array list so that you can individually assess each word.

TheTriad
  • 9
  • 1