Remove First Character From String Python (5 Ways)

remove first character from string in python
remove first character from string in python

In this article, we will be solving a problem statement on how to remove first character from string Python. We will be discussing the steps and python methods required to remove the first character from the string.

Method 1- Remove first character from string Python using Slicing

String Slicing in python means getting a substring from the given string by slicing it by providing the start and end index. To know more about string slicing you can visit here.

We specify the start index and end index separated by a colon to return the sub-string of the given string.

In this case, we need to remove first character from string, hence we slice string with start index as 1, and since we need the whole string except the first character hence we do not provide any end index. i.e [1: ]

Python Code

# define the string
my_string = "Hello World.!"

#Using String Slicing
new_string = my_string[1:]
print("String after removing first character from string is ->", new_string)

Output:

String after removing first character from string is -> ello World.!

Read Also:

Method 2- Remove first character from string Python using lstrip()

Python lstrip() method returns a copy of the string, in which the leading characters are removed based on arguments that were passed.

Syntax:

string.lstrip(character)

lstrip() method parameters: We can specify the character or if arguments are not provided it will remove all the leading white spaces from the string.

Hence we can remove first character from string Python by providing the first character as the argument of the lstrip() method. This method will return a copy of the sting without the first character.

Python Code

# define the string
my_string = "Hello World.!"

#first character
first_character = my_string[0]

#Using String Slicing
new_string = my_string.lstrip(first_character)
print("String after removing first character from string is ->", new_string)

Output:

String after removing first character from string is -> ello World.!

Method 3- Remove first character from string Python using split() and join() method

The same problem can be solved by using split() and join() methods. The steps to remove first character from string in Python are:

  • Get the first character of the string using index 0
  • Split string using the split() method, with separator as the first character and max split argument as 1.
  • Join the array which is returned by the split() method using the join() method.
  • Print the new string

Python Code:

# define the string
my_string = "Hello World.!"

# first character
first_character = my_string[0]

# spliting string with separtor as first charqcter and max split as 1
# And joining the array using join()
new_string = "".join(my_string.split(first_character, 1))
print("String after removing first character from string is ->", new_string)
String after removing first character from string is -> ello World.!

Method 4- Remove first character from string Python using regex

Regular Expression called regex is a specialized sequence of characters that helps to find a specific string or string set. Python re module provides support for regular expressions. To know about the re module of python you can visit here.

Python code:

# importing regex module
import re

# define the string
my_string = "Hello World.!"

new_string = re.sub(r'.', '', my_string, count = 1)
print("String after removing first character from string is ->", new_string)

In the above sub() method, the count = 1 indicates the substitution will be done only for the first occurrence of the matching string.

Output:

String after removing first character from string is -> ello World.!

Method 5 – Remove first character from string Python using replace() method

Python string replace() method is an in-built method that returns the copy of a string, where the occurrences of the substring are replaced with the given string as the argument.

Syntax:

string.replace(old, new, count)

Parameters:

  • old – substring that needs to be replaced
  • new – substring that will be replacing the old substring.
  • count – This is an optional argument, it is the number of counts we need to replace the old string with the new string.

Hence to remove first character from string python we can use replace() method, in which the arguments are given as old substring = first_character, new substring = empty string ie. “”, and count = 1. The count is given as 1 because we want to remove only the first character. ie string.replace(first_charcter, "", 1)

Python Code:

# define the string
my_string = "Hello World.!"

# getting first character
first_character = my_string[0]

new_string = my_string.replace(first_character, "", 1)
print("String after removing first character from string is ->", new_string)

Output:

String after removing first character from string is -> ello World.!

Conclusion

Above we can remove first character from string python by using slicing, split() method, lstrip() method, using regex and replace() method.

Similar Posts