Python String Remove First Character

Here, we will develop a Python program to string remove the first character. If the string was “Knowprogram” then the new string will be “nowprogram”. We will discuss how to remove the first character from the given string using native methods, slice operator, and Regular Expression.

Python Replace First Character in String

We will take a string while declaring the variables. Then, We will run the loop from 1 to len(string) and append the string into the empty string. Calculated the length of the string using len() function to access the character of the string. Finally, a new string will be displayed on the screen.

# Python program to remove first character from string

# take input
string = input("Enter any string: ")

# remove first character from string
out_string = ""
for i in range(1, len(string)):
    out_string = out_string + string[i]
   
# print string after removal
print ("New string:", out_string)

Output for the different input values:-

Enter any string: Python
New string: ython

Enter any string: Know Program
New string: now Program

Python Remove First Character using Slicing

We will remove the first character from the string using the slice operator. By slicing it from the 1 index, we have removed the first character of the string. The string[1:] specifies all the characters of the string except the first one. The [1:] specifies the character at index 0 and goes up to the index after the first one.

# Python program to remove first character from string

# take input
string = input("Enter any string: ")

# remove first character using slicing
out_string = string[1:]
   
# print string after removal
print ("New string:", out_string)

Output:-

Enter any string: slicing
New string: licing

Python Remove 1st Character from String

We can use the Regular Expression or regex() function in python, to match 2 groups in a string i.e.

  • Group 1: First character of the string
  • Group 2: Every character in string except the first characters

We will first be importing the RegEx module. Take a first_group function which will only return only group 1 from the match object delete other groups. Then, we have applied the function with the proper syntax and tried to remove the first character only by putting value = 1 in the given syntax. The sub() function replaced the matched characters in the string with the characters returned by the first_group() function.

# Python program to remove first character from string

# importing RegEx module
import re

def first_group(m):
    ''' Return only group 1 from the match object
        Delete other groups '''
    return m.group(2)

# take input
string = input("Enter any string: ")

# remove first character using re.sub()
out_string = re.sub("(^.{1})(.*)", first_group, string)
   
# print string after removal
print ("New string:", out_string)

Output:-

Enter any string: RegEx
New string: egEx

Also See:- Python Program to Check Special Character

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *