The built-in str.partition() method will do this for you. Unlike str.split() it won't bother to cut the rest of the str into different strs.

text = raw_input("Type something:")
left_text = text.partition("!")[0]

Explanation

str.partition() returns a 3-tuple containing the beginning, separator, and end of the string. The [0] gets the first item which is all you want in this case. Eg.:

"wolfdo65gtornado!salmontiger223".partition("!")

returns

('wolfdo65gtornado', '!', 'salmontiger223')
Answer from Michael Hoffman on Stack Overflow
Discussions

Read file until string match?

files are iterators (unlike lists) so you can just make a second for loop:

with open(inputfile, 'r') as infile:
    for line in infile:
        if line.startswith(">"):
            print line, # comma on the end prevents the double spacing from printing a file line
            for line in infile:
                print line,
                if line.startswith(">"):
                    break # stop this inner for loop; outer loop picks up on the next line

Edit: I get the feeling I don't understand your problem. Can you show some example data and what you want out?

More on reddit.com
๐ŸŒ r/learnpython
13
9
February 1, 2017
text - Read Up Until a Point Python - Stack Overflow
I have a text file full of data that starts with #Name #main then it's followed by lots of numbers and then the file ends with #extra !side So here's a small snippet #Name #main 60258960 33031... More on stackoverflow.com
๐ŸŒ stackoverflow.com
regex - Read file until specific line in python - Stack Overflow
I have one text file. I am parsing some data using regex. So open the file and read it and the parse it. But I don't want to read and parse data after some specific line in that text file. For exam... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you read up to a specific character in a line of text? - Post.Byes
I have a line of characters that need to be separated. One example is like this: 513413;dialog_513413;Sally Mae has some jobs for you.; Three sets of data all split into three groups placed in one line. What I would like to do is be able to read each group up to the semicolon (obviously without ... More on post.bytes.com
๐ŸŒ post.bytes.com
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-read-file-until-specific-character
Read a file until a specific Character in Python | bobbyhadz
April 10, 2024 - Read a file until a specific Character in Python ยท Read a file until a specific Character using a while loop ยท To read a file until a specific character: Open the file in reading mode.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ read file until string match?
r/learnpython on Reddit: Read file until string match?
February 1, 2017 -

I have this code:

with open(inputfile, 'r') as infile:
    for line in infile:
        if line.startswith(">"):
            print line
            # keep printing until next ">" encountered; stop at that point
            # and move on to next line that starts with ">"...

But I don't know how to do that last part...keep printing lines in the file until I hit the next ">", at which point I need it to stop printing, find the next line that starts with ">", then start printing again.

I've searched like every google link for this. It has been asked a lot, but nowhere is there an explanation understand. Usually it's also some variation, such as print from the start until match, but I know how to do that. I need to print from match to match...

I think I can do this with enumerate() a list, and a while loop, but it requires I read the file into memory, which I don't want to do considering the files I need to work on are a few GB each and contain a few million lines, so I don't want to read the entire thing into a list...

The real problem I am having is how to access the "next line" after the line that contains ">"? next(infile) works, but only for the immediate next line. What if I need the next 2 or 3 lines? I tried the following inside the "if line.startswith" part:

line = next(infile)
while not line.startswith(">"):
    print line
    line = next(infile)

But that doesn't work...(not entirely sure why, I assume next() can only take the immediate next line).

Anyone? Is there no default python function (that I can't find) that does this?

๐ŸŒ
PyTutorial
pytutorial.com โ€บ read-string-until-character-in-python
PyTutorial | Read String Until Character in Python
February 11, 2025 - The split() method is a simple way to read a string until a specific character. It splits the string into a list based on the delimiter.
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ print string till character in python
Print String Till Character in Python [4 ways] - Java2Blog
November 25, 2022 - A break statement is used to break out of the current loop when encountered. To print string till character in Python, we can iterate over the string and print individual characters. If the specified character is encountered, we can break out of the loop using the break statement.
๐ŸŒ
Bytes
bytes.com โ€บ home โ€บ forum โ€บ topic โ€บ python
How do you read up to a specific character in a line of text? - Post.Byes
September 6, 2010 - I am aware of string.read(siz e), but obviously size is needed and it is variable. Highly prefer having size given also.
Find elsewhere
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python | split string until character/substring
Python | Split String until Character/Substring - Be on the Right Side of Change
December 15, 2022 - Approach: Use the re.findall method to find all the characters that appear until the last occurrence of the character โ€œ/โ€. To also include the โ€œ/โ€ character in the final string you can specify the pattern within parenthesis which will also include the โ€œ/โ€. In case of the first example ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-string-till-substring
Python - String till Substring - GeeksforGeeks
January 16, 2025 - We can manually iterate through the string and stop when we encounter the substring. ... s = "learn-python-with-gfg" sub = "-" res = "" for c in s: # Iterating through each character in the string if c in sub: # Stop when the substring is found break res += c # Add characters to the result print(res)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-extract-string-till-all-occurrence-of-characters-from-other-string
Python - Extract String till all occurrence of characters from other string - GeeksforGeeks
July 23, 2025 - Otherwise, it calls itself recursively with idx+1. The function keeps calling itself recursively until all characters of check_str are present in the temporary substring or until the end of test_str is reached. Below is the implementation of the above approach: ... # Python program for the above approach # Function to extract string from the given # string till any characters def extract_till_chars(test_str, check_str, idx=0): if idx == len(test_str): return "" temp = test_str[:idx+1] if all(char in temp for char in check_str): return temp else: return extract_till_chars(test_str, check_str, idx+1) # Driver Code test_str = "geeksforgeeks is best for all geeks" check_str = "freak" res = extract_till_chars(test_str, check_str) print("The original string is : " + str(test_str)) print("String till all characters occurred: {}".format(res))
๐ŸŒ
Tutorial Reference
tutorialreference.com โ€บ python โ€บ examples โ€บ faq โ€บ python-how-to-read-file-until-a-specific-character
How to Read a File Until a Specific Character in Python | Tutorial Reference
If your file is relatively small and fits comfortably in memory, the simplest way to read up to a specific character is to read the entire file and then use the split() method: with open('example.txt', 'r', encoding='utf-8') as file: ... The with open() opens the file in read mode ('r') with ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-read-character-by-character-from-a-file
Python program to read character by character from a file - GeeksforGeeks
September 6, 2024 - Input: Geeks Output: G e e k s Explanation: Iterated through character by character from the input as shown in the output. In this article, we will look into a few examples of reading a file word by word in Python for a better understanding of the concept.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ string โ€บ python-data-type-string-exercise-19.php
Python: Get the last part of a string before a specified character - w3resource
June 12, 2025 - Write a Python program to split a string at the first occurrence of a specified character and return the substring before it. Write a Python program to use the partition() method to extract the part of a string before a given delimiter. Write a Python program to iterate over a string and build the substring until a specified character is encountered.
๐ŸŒ
Finxter
blog.finxter.com โ€บ how-to-read-one-character-at-a-time-from-a-file-in-python
How to Read One Character at a Time from a File in Python? โ€“ Be on the Right Side of Change
The lambda function uses read() to read one character at a time. The results of each iteration are output to the terminal. Then, sleep() is called and passed an argument, the number of seconds to delay execution. The iteration continues until all characters in the file have been output to the ...
๐ŸŒ
Pretagteam
ww25.pretagteam.com โ€บ question โ€บ get-string-until-character-python
Pretagteam
September 21, 2021 - We cannot provide a description for this page right now