Python Forum
Reading a text until matched string and print it as a single line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a text until matched string and print it as a single line
#1
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.
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print on a single line with start/end brackets and commas Paulman 2 1,864 Oct-23-2021, 10:00 AM
Last Post: Paulman
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,610 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Appending to a text string syno7878 10 4,260 May-03-2020, 10:05 AM
Last Post: snippsat
  Using an integer to manipulate a string/text variable rexyboy2121 1 1,750 Apr-22-2020, 01:37 AM
Last Post: michael1789
  Fetch student details from a text file and print the details. Pranav 2 6,250 Mar-17-2020, 09:36 AM
Last Post: Pranav
  Trying to extract Only the capitol letters from a string of text Jaethan 2 2,181 Feb-27-2020, 11:19 PM
Last Post: Marbelous
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,553 Feb-27-2020, 09:22 AM
Last Post: buran
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,921 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  finding p's in words of a multi-line text file johneven 5 4,369 Jun-24-2019, 03:41 PM
Last Post: ThomasL
  reading text file and writing to an output file precedded by line numbers kannan 7 10,413 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020