Python remove first character from String [6 Methods]

In this tutorial on Python, I will explain how Python remove first character from string using different methods. Also, I will explain all the methods in detail with the help of some demonstrative examples.

In Python, strings are immutable. This means that once we create a string object, we cannot change its contents. If we want to make modifications to a Python string, such as appending characters or replacing substrings, we create a new string with the desired changes rather than modifying the original Python string in place.

Python offers a multitude of methods to manipulate and transform strings, given that they are one of the most commonly used data types. One such common task is to remove first character from string Python.

Methods in Python remove first character from String

There are many different ways to remove the first character from a Python string:

  1. String slicing
  2. lstrip() function
  3. List comprehension
  4. replace() method
  5. Regex module
  6. split() and join() function

Let’s see them one by one using some illustrative examples:

Method 1: Remove first character from string Python using String slicing

Strings in Python are essentially sequences of characters. This means they can be indexed and sliced similar to how lists and arrays are treated.

By slicing a string, we can extract a portion or ‘slice’ of the Python string based on indices. To remove the first character, we would start the slice from the second character and go up to the end of the string. This gives us a new string without the first character in Python.

We can use both positive and negative indexing in Python remove first characters from string. The syntax of string slicing is:

string(start:end:step)
NameDescription
startThe beginning index of the slice. It’s inclusive, which means the slice starts at this index. If omitted, it defaults to 0 for positive steps and -1 for negative steps.
endThe ending index of the slice. It’s exclusive, which means the slice goes up to but does not include this index. If omitted, it defaults to the end of the string for positive steps and the beginning of the string for negative steps.
stepThe interval between characters in the slice. If omitted, it defaults to 1.
Parameters in Python string slicing.

Example 1: Suppose someone typed the name of a U.S. state but mistakenly added an extra character at the beginning. We’ll use string slicing to remove that first character from the string in Python.

entry = "zTexas"
corrected_entry = entry[1:]
print('Corrected entry data: ', corrected_entry)

Output: We have used only the positive indexing in Python string slicing.

Start Index (1): This tells Python to start slicing from the second character of the string (remember, indexing in Python starts at 0). In our entry string, the character at index 1 is ‘T’.
Stop Index (omitted): Since we don’t provide a stop index, the slice continues until the end of the string by default in Python.
Parameters in the string slicing.
Corrected entry data:  Texas
python remove first character from string

This way we can use positive index in string slicing in Python remove first character from string.

Example 2: Suppose each license plate in a US database incorrectly starts with an “X”. We need to remove this “X” through Python using string slicing with negative indexing:

license_plate = "XABC123"
corrected_license_plate = license_plate[-len(license_plate)+1:]
print(corrected_license_plate)

Output: Here, we are using the len() function which will return the length of the Python string. Making the length of the string value negative and adding 1 to it means, we are getting the negative index value of the 2nd character in the Python string.

In this case, the length of the string is 7, and the value of -7+1 = -6. which is the negative index of the 2nd character in the string. and we are using this value as the start parameter of the string-slicing in Python.

Other things are the same as the positive index string slicing in Python.

ABC123
python delete first character from string

This way we can use the positive index in string slicing in Python remove first character from string.

Method 2: Python remove first character from string using lstrip() function

The lstrip() is a built-in method for strings in Python. Its primary purpose is to remove leading white spaces from the start of a string in Python.

If we provide a character or set of characters as an argument, lstrip() will remove these leading characters from the beginning of the string in Python. However, if the first character repeats multiple times consecutively, it will remove all such instances.

Scenario: Consider a situation where we are processing bank check amounts stored as strings in Python. Due to an error, each string starts with a $ symbol, which needs to be removed using lstrip() in Python.

check_amount = "$1200.45"
cleaned_amount = check_amount.lstrip('$')
print(cleaned_amount)

Output: The lstrip() function is used to remove leading characters (characters at the start of a string). By providing the $ as an argument to lstrip(), The lstrip() function is used to remove leading characters (characters at the start of a string), we are asking Python to remove the $ symbol from the beginning of the string.

1200.45
python string remove first character

The lstrip() function remove first character from string Python.

Method 3: Remove first character string Python using list comprehension

List comprehension is a concise way in Python to create lists. It provides a syntactical approach to generate lists based on existing lists or strings in Python.

By using list comprehension, we can iterate over each character in the string in Python. We can then choose to exclude the first character and include the rest in a new list. After this, we would concatenate the characters together to form a new string without the first character.

Example: Consider a situation in which we’re cataloging books in a library in Washington, D.C. For older books, their ID starts with the letter ‘O‘ which now needs to be removed for digital cataloging through Python.

book_id = "O19876"
clean_id = ''.join([book_id[i] for i in range(1, len(book_id))])
print(clean_id)

Output: A list comprehension in Python is used here to iterate over each character in the string book_id except the first one. The comprehension [book_id[i] for i in range(1, len(book_id))] generates a list of characters, excluding the first. The ”.join() method is then used to concatenate those characters into a string in Python.

19876
how to remove first character from string in python

This is the way we can use list comprehension with the join() method in Python remove first character from string.

Method 4: Python string without first character using replace() method

The replace() method is a built-in function for strings in Python. It is used to replace a specified substring with another substring.

By specifying the first character of the Python string as the target for replacement and providing an empty Python string as the replacement, we can effectively remove the first character. There is an option to limit the number of replacements, ensuring only the first character is targeted.

Scenario: In this scenario, we are journalists in the USA working on an article about historic landmarks. Every landmark in our document starts with an asterisk (*) for emphasis, but we decide it’s not necessary.

landmark = "*Statue of Liberty"
clean_landmark = landmark.replace("*", "", 1)
print(clean_landmark)

Output: The replace() method is used to replace occurrences of a specified substring. The arguments “*”, “”, 1 mean “replace the first occurrence of ‘*’ with an empty string”.

Statue of Liberty
remove first letter from string python

This way we can use the replace() function in Python remove first character from string.

Method 5: Python remove first letter from string using the regex module.

Regular expressions (often abbreviated as “regex”) are sequences of characters that define search patterns in Python. Python has a built-in module named re that allows for the use of regular expressions.

By crafting a regular expression pattern that matches the start of a Python string and any character following it, we can replace this pattern with an empty string in Python using the sub() function in re module, effectively removing the first character.

Example: Working for the USPS, we notice some ZIP Codes have an erroneous ‘@‘ symbol at the start. This needs correction for accurate mail delivery through Python with the help of regular expression’s sub() function.

import re
zip_code = "@90210"
clean_zip = re.sub('^@', '', zip_code)
print(clean_zip)

Output: Regular expressions provide a way to search and manipulate strings in Python based on patterns. Here, re.sub(‘^@’, ”, zip_code) replaces the “@” character at the start of the string (denoted by ‘^‘) with an empty string in Python.

90210
string remove first character python

The regular expression’s sub() function is used in Python remove first character from String.

Method 6: Python remove first element from string using split() and join() function

The split() function is a method in Python that divides a string based on a specified delimiter and returns a Python list of substrings. And, the join() function is a method used to concatenate that Pythion list of strings into a single string based on a specified delimiter in Python.

Scenario: For the scenario of removing the dollar sign from the beginning of a sensor reading, you can achieve this in one line using the split() and join() functions as follows

data = "$256.43"
clean_data = ''.join("$256.43".split("$")[1:])
print(clean_data)

Output: The “$256.43”.split(“$”) pile of code, will split the string at each dollar sign, resulting in a Python list: [”, ‘256.43’]. Then, using list slicing [1:], we’re selecting all elements starting from the second element. Finally, join() merges the selected elements back into a string in Python.

256.43
remove first letter of string python

This way we have used the split() and join() functions to remove first character from string.

Conclusion

This article was based on how Python remove first character from string using seven different methods like string slicing, lstrip() function, list comprehension, replace() method, regex module, and split() with join() functions with some illustrative examples.

The choice totally depends on the user, which method to choose to remove the first character from the string in Python.

You may like the following Python tutorials: