Table of Contents
A string can be termed as a collection of characters with each character occupying a particular position. Strings cannot be altered and are considered immutable in Python.
Print String Till Character in Python
This tutorial will demonstrate how to print string till character in Python.
Using the for
loop and break
statement
A string can be iterated over using a for
loop in Python. 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. We will check if the specific character is encountered using the if
statement.
See the code below.
1 2 3 4 5 6 7 8 |
a = "Java2Blog" for i in a: if(i=='2'): break else: print(i) |
a
v
a
In the above example, we have a string Java2Blog
and we print the string till character 2
is encountered. When this character is encountered, we break out of the loop.
Using the slicing technique
The slicing technique uses indexes to extract portions from a linear collection like a string or a list. We can specify the start and stop positions of the elements to be extracted in square brackets.
The index()
function is used to get the index of a particular element in a string. To print string till character in Python, we will first use this function to get the index of the required character and then use string slicing to print the string till this index.
For example,
1 2 3 4 5 |
a = "Java2Blog" i = a.index('2') print(a[:i]) |
Output:
Using the split()
function
The split()
function can be used to split a string based on a specific character. It splits the string into various substrings.
To print string till character in Python, we will split it based on the required character. This will create two substrings and we will display the first one.
We implement this logic below.
1 2 3 4 5 |
a = "Java2Blog" b = a.split('2') print(b[0]) |
Output:
Further reading:
Using the partition()
function
The partition()
function works similarly to the previous method. We are able to create partitions of a string based on a substring. This function returns a tuple containing three elements. The first element is the partition before the substring, the second element is the substring, and the final element is the partition after the substring.
See the code below.
1 2 3 4 5 6 |
a = "Java2Blog" b = "2" c = a.partition(b) print(c[0]) |
Output:
Conclusion
To conclude this tutorial, we discussed several methods to print string till character in Python. In the first method, we iterated over a string and displayed the characters of the string till the specified character is encountered. The break
statement was used to break out of the loop.
In the next method, we used the slicing method to display portions of the string and used the index()
function to get the index of the specified element. The last two methods split the string into substrings and work similarly.