How to remove first character from a string in Python

In this Python tutorial, we will discuss how to remove first character from a string in Python in different ways. Consider the fact that a Python string is immutable, which means it cannot be changed.

  1. Remove the first character from a string using the slicing method in python
  2. Remove the first character from a string using replace method in python
  3. Remove the first character from a string using the lstrip() method in python
  4. Remove the first character from a string using the translate method in python
  5. Remove the first character from a string using the regex method in python
  6. Remove the first character from a string using the split function in python
  7. Remove the first character from a string using the join and list comprehension method in python
  8. How to remove the first character from the string Python Pandas
  9. How to remove the first n character from a string in Python
  10. How to remove the first character from a string in a list

Remove first character from a string using the slicing method in python

  • In this example, we will discuss how to remove the first character from a string in Python by using the slicing method.
  • The slicing technique can be used to remove the first character from a Python string, and the resulting string will be generated without the first character. By using the slicing [1:] we will get the remaining string except for the first character.

Example:

Let’s take an example and check how we can remove the first character from a string in Python by using the slicing method.

# Input string
Country_name = "United States of America"
print("Input string: ", Country_name)

# By using the slicing method
result = Country_name[1:]
print("First character has been removed from string: ", result)

In the following given code first, we will create an input string named a country_name in which we assigned the string value to the United States of America (USA).

Now we want to remove our first character which is ‘U’ for this we will use the concept of str[:1] and get the remaining string except for the first character.

You can refer to the below Screenshot

How to remove the first character from a string in Python
How to remove the first character from a string in Python

This is how to remove the first character from a string in Python by using the slicing method.

Read How to get string values from list in Python

Remove the first character from a string using replace method in python

  • In this section, we will understand how to remove the first character from a string in Python by using the replace() method.
  • When replacing a single character with a new character in Python, use the replace() method, and this method will produce a copy of the string that replaces every instance of one substring with another substring.
  • In this example if we want to remove the first character from the string then we have to mention the second parameter is an empty string.

Syntax:

Let’s have a look at the syntax and understand the working of the str.replace() method in Python.

str.replace(old,  new, count)
  • It consists of a few parameters
    • old:- This parameter defines the string which will be replaced.
    • new:- Replace the existing value with a new one (a character or a string).
    • count:- an integer value that indicates how many instances of the old character or substring you want to replace with the new one. It is an optional parameter.

Example:

Here we will take an example and check how to remove the first character from a string in Python by using the replace() method.

Source Code:-

# Input string
state_name_in_USA = 'Alaska'

# Using replace() method
result= state_name_in_USA.replace('A', '')

# Display the content
print(result)

In the above example, we used the str.replace() function, and within this function, we have mentioned the old character ‘A’ and replaced it with a new character but in this case, we have to remove the character so, the second parameter will be an empty string.

Here is the implementation of the following given code

How to remove the first character from a string in Python using replace
How to remove the first character from a string in Python using replace

As you can see in the screenshot we have discussed how to remove the first character from a string in Python by using the replace() method.

Read Python split string by space

Remove the first character from a string using the lstrip() method in python

  • Here we will understand how to remove the first character from a string in Python by using the str.lstrip() method.
  • After removing the leading characters supplied as input, the lstrip() method returns a copy of the string. If there is no argument given, it erases the leading whitespace by default.

The example code below shows how to remove the string’s beginning characters using the str.lstrip() method.

Source Code:-

# Input String
Cars_in_USA = "Ford F-150, Chevrolet Silverado 1500"
print("Input string:",Cars_in_USA)

# By using the lstrip()
result = Cars_in_USA.lstrip("F")

# Display the Content
print("Remove first character:",result)

In the following given code first, we have declared a variable ‘Cars_in_USA’ and then assign the top most cars in the USA. Now we want to remove the first character for this we are going to use the lstrip() function and within this function, we passed the first character to it which we want to remove from the input string.

Here is the Screenshot of the following given code

How to remove the first character from a string in Python using lstrip method
How to remove the first character from a string in Python using the lstrip() method

This is how we can remove the first character from a string in Python by using the lstrip method.

Read Python find number in String

Remove the first character from a string using the translate method in python

  • In this section, we will understand how to remove the first character from a string in Python by using the translate() method.
  • The translate() function of Python is used to return a string in which each character is mapped to its associated position in the translation table.
  • To remove a character from the result string, we must give the character’s Unicode code point and the word “None” as a replacement. To determine a character’s Unicode code point, we can utilize the ord() method.

Syntax:

Here is the Syntax of the string.translate() function in Python.

string.translate(table)

Note: This parameter takes only one parameter and defines a translation table storing the mapping between two characters.

Example:

Let’s take an example and check how to remove the first character from the string in Python by using the translate() method

# Input string
cities_of_USA = 'New York, Los Angeles, California'
print("Original string:",cities_of_USA)

# Using the translate() method
result= cities_of_USA.translate({ord('N'): None})
print("First character removed:",result)

Here is the execution of the following given code

How to remove the first character from a string in Python using translate method
How to remove the first character from a string in Python using the translate method

In this example, we have understood how to remove the first character from a string in Python by using the translate method.

Remove the first character from a string using the regex method in python

  • Now that we’ve covered the slicing() and str.lstrip() methods, it’s time to look at the regex() function in python. The regex module’s ‘re’ class has the function re.sub(). The initial character can also be removed using it. 
  • Re.sub() creates a string with replaced values and stands for a substring. When we apply this function, we can replace several elements and it will replace all the characters that match the regular expression.

Syntax:

Let’s have a look at the Syntax and understand the working of the re.sub() method in Python.

re.sub(pattern, repl, string, count=0, flags=0)
  • It consists of a few parameters
    • pattern: This parameter defines the string/pattern that we want to be replaced.
    • repl: This parameter is used to define the pattern with which the pattern is replaced.
    • Count: By default, it takes a 0 value and it defines the number of replacements that should occur.

Example:

Here we will take an example and check how to remove the first character from a string in Python.

Source Code:

import re
# input string
Country_name = "U.S.A, Australia, Belgium"

# By using the re.sub() method
result = re.sub(r'.', '', Country_name, count = 1)

# Display the content
print(result)

As a result, we will import the re library first to verify that the function, which is found in the re module, and Then, we produce a string.

The re.sub() command is then executed, with the parameters string and count passed in it. Here, we utilize the expression “count=1” to indicate that character removal.

You can refer to the below Screenshot

How to remove the first character from a string in Python using regex method
How to remove the first character from a string in Python using the regex method

This is how we can remove the first character from a string in Python by using the regex method.

Read Python string to list

Remove the first character from a string using the split function in python

  • In this example, we will discuss how to remove the first character from a string in Python by using the split() function.
  • This function is used to split the string into a substring after splitting the given string by the specified separator.
  • This parameter takes two parameters and specifies the separator and maxsplit. It will return a list of strings after splitting the input string.

Example:

Bikes_in_USA = 'Aprilia, Kawasaki, Ducati'
print("Input string:",Bikes_in_USA)
#Using the split() function

result= Bikes_in_USA.split("A")
print("First character remove from string:",result)

You can refer to the below Screenshot

How to remove the first character from a string in Python using split function
How to remove the first character from a string in Python using the split function

This is how to remove the first character from a string in Python by using the split function

Remove the first character from a string using the join and list comprehension method in python

  • In this section, we will discuss how to remove the first character from the string in Python by using the list comprehension and the join() method.
  • A string and an iterable object are concatenated using the join() method in Python. It returns a product string that combines all of the strings in the iterable.
  • By using the list comprehension and join method we can easily involve splitting each string element into its associated list element, then joining each of them to declare a new string.

Example:

Country_name = "U.S.A, Germany, Australia"
 print ("Input string: ", Country_name)

# By using the join() method 
output = ''.join([Country_name [i] for i in range(len(Country_name )) if i != 0]) 

# Display the Content   
print ("First character remove from string: ",output) 

In the following given code first, we created an input string named “Country_name” and within this, we assigned the country name to it. Next, we used the join() method and iterate the values with a set condition if i !=0 and it will remove the first character from a string.

Here is the execution of the following given code

How to remove the first character from a string in Python using join method
How to remove the first character from a string in Python using the join method

As you can see in the Screenshot we have discussed how to remove the first character from a string in Python by using the join and list comprehension method.

Read Remove character from string Python

How to remove the first character from the string Python Pandas

  • Here we will discuss how to remove the first character from the string in Python Pandas.
  • To perform this particular task we are going to use the slicing method the slicing technique can be used to remove the first character from a Python string, and the resulting string will be generated without the first character. By using the slicing [1:] we will get the remaining string except for the first character.

Example:

Let’s take an example and check how we can remove the first character from the string in Python Pandas

import pandas as pd

df = pd.DataFrame({"Country_name":["U.S.A"]})
df
df["Country_name"].str[1:]

In the above code first, we imported the Pandas library and then create a dataframe by using the pd.dataframe() and within this function, we assign the country_name. Next, we used the slicing method and will remove the first character from the string.

You can refer to the below Screenshot

How to remove the first character from the string Python Pandas
How to remove the first character from the string Python Pandas

This is how to remove the first character from the string in Python Pandas.

How to remove the first n character from a string in Python

  • In this section, we will discuss how to remove the first n character from a string in Python.
  • If you want to remove the first n characters from the string then Slice a string starting at the nth index and ending at the end. The first n characters of a string would be removed if it were sliced from the nth index to the end because characters in a string are indexed starting from zero.

Example:

Here is the example we will understand how to remove the first n character from a string in Python.

# Input string
Country_name = "United States of America"
print("Input string:",Country_name)

# By using the slicing method
result = Country_name[5:]
print("First character has been removed from string:",result)

In the above code, we have set the n=5 and it will remove the first 5 characters from an input string.

Here is the Implementation of the following given code

How to remove the first n character from a string in Python
How to remove the first n character from a string in Python

This is how to remove the first n character from a string in Python.

Read How to create a string in Python

How to remove the first character from a string in a list

  • In this example, we will understand how to remove the first character from each string in a Python list.
  • When replacing a single character with a new character in Python, use the replace() method, and this method will produce a copy of the string that replaces every instance of one substring with another substring.
  • In this example, if we want to remove the first character from the string in the list and then iterate the values using the for loop method.

Example:

Here we will take an example and check how we can remove the first character from a string in a Python list.

cities_in_USA = ['New York', 'Los Angeles', 'Chicago', 'Houston']

# Display original list
print("Input list : ", cities_in_USA)

new_output = []
for m in cities_in_USA:
	m=m.replace(m[0],"",1)
	new_output.append(m)

# Display the result
print("First character has been removed from list :", new_output)

Here is the Screenshot of the following given code

How to remove the first character from each string in a list
How to remove the first character from each string in a list

This is how to remove the first character from each string in a list.

This article was based on how to remove the first character from a Python string. Seven different approaches have been presented and all the methods are very simple and easy to understand.

  • Remove the first character from a string using the slicing method in python
  • Remove the first character from a string using replace method in python
  • Remove the first character from a string using the lstrip() method in python
  • Remove the first character from a string using the translate method in python
  • Remove the first character from a string using the regex method in python
  • Remove the first character from a string using the split function in python
  • Remove the first character from a string using the join and list comprehension method in python
  • How to remove the first character from the string Python Pandas
  • How to remove the first n character from a string in Python
  • How to remove the first character from a string in a list

You may like the following Python tutorials: