In this article, we will discuss different ways to remove first characters from a string.
Remove first character from string in python using slicing
In python, we can slice a string to select a range of characters from it. For example,
str[start:end]
start and end are the index positions. The above expression returns a new string containing characters from index position start to end -1. The default values of start & end are 0 & N respectively, where N is the size of the string. So, if neither start nor the end positions are provided like some_str[:], then it returns a new string containing all the characters of the original string.
We can use this to remove first character from string i.e.
sample_str = sample_str[1 : ]
It selects the characters from index position 1 to end and creates a new string containing them. Then we assigned back that string to the same string variable, it gave an effect that we have deleted the first character of string.
Complete example is as follows,
sample_str = "Sample String" # Slice the string to remove first character sample_str = sample_str[1 : ] print(sample_str)
Output:
ample String
It deleted the first character from string.
Remove first character from string in python using Regex
We can use the regex modules’s sub() function to delete the first character of string. In regex, the the re.sub() function matches the given pattern in the string and replaces the matched characters with a given replacement string.
To delete the first character from string using rege’s sub() function, you can pass a pattern that selects the first character of string only and as a replacement string pass the empty string. For example,
sample_str = re.sub("^.", "", sample_str)
It will select the first character of string, replace it with the given replacement string i.e. empty string. Finaly returns a new string containing remaining characters. Then we assigned back that string to the same string variable, it gave an effect that we have deleted the first character of string.
Complete example is as follows,
import re sample_str = "Sample String" # Remove first characters from string sample_str = re.sub("^.", "", sample_str) print(sample_str)
Output:
ample String
It deleted the first character from string.
Python: Remove first character from string if match
In some scenarios, it might be possible that you want to delete the first character from a string only if it is amatch to another character. For example, lt’s see how to remove first character from string if it is ‘S’,
# Example 1 sample_str = "Sample String" char_to_check = 'S' # Remove first character from string if matches the given character if char_to_check == sample_str[0]: sample_str = sample_str[1:] print(sample_str) # Example 2 sample_str = "An apple a day" char_to_check = 'S' # Remove first character from string if matches the given character if char_to_check == sample_str[0]: sample_str = sample_str[1:] print(sample_str)
Output:
ample String An apple a day
In first example, it deleted the first character from string because it was ‘S’. But in second example it didn’t deleted the first character because it was not ‘S’
Summary:
We learned about different ways to delete first character from a string in python.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.