Reading a text until matched string and print it as a single line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Reading a text until matched string and print it as a single line (/thread-31227.html) |
Reading a text until matched string and print it as a single line - cananb - Nov-29-2020 Hi everyone, I am trying to write a program as a python beginner. This function scans the given text. Whenever I come across the symbols “@, &, % or $”, I have to take the text until that point and print it as a single line. Also, I have to skip the symbol, start the next line from the letter right after the symbol and print it until I come across another symbol. I hope I told it well. symbols= "@, &, %, $”" for symbol in text: if text.startswith (symbols): print (text)I know it's not correct but that was all I could think. Any help is appreciated. RE: Reading a text until matched string and print it as a single line - DPaul - Nov-29-2020 in line 1, there is an extra symbol at the end, typo or intended? its easier when you take some text like this: text = 'vgfs@bfq&bkjmlqajyre%vhreafmze$nhvkle&hcvjo' It will always fail in line 3 because it does not "startswith(..) any of the symbols. I would create a loop that iterates over all the items in the text. Check if they are in "symbols". If they are not : accumulate the line so far, if you find a symbol, print the line. Paul |