Remove first n characters from a string
In Python, you can remove the first 'n' characters from a string by using slicing. The slice notation [n:] can be used to select all characters in a string starting from the nth index.Using Python slicing
Here is an example of how you can use Python slicing to remove the first 2 characters from a string:
original_string = "Hello, World!"
new_string = original_string[2:]
print(new_string)
// Output: "llo, World!"
Using negative indexing
You can also use negative indexing to remove characters from the end of the string.
original_string = "Hello, World!"
new_string = original_string[:-2]
print(new_string)
// Output: "Hello, Wor"
Using string.lstrip() method
You can also use string.lstrip() method to remove specific characters from the left side of the string.
original_string = "Hello, World!"
new_string = original_string.lstrip("H")
print(new_string)
// Output: "ello, World!"
Note that this method only removes characters from the left side of the string, if you need to remove from both side use string.strip() method.
original_string = "Hello, World!"
new_string = original_string.strip("H!")
print(new_string)
// Output: "ello, World"
Using string.replace() method

You can also use the string.replace() method to remove specific characters from a string.
original_string = "Hello, World!"
new_string = original_string.replace("H","")
print(new_string)
// Output: "ello, World!"
These are some examples of how you can remove the first n characters from a string in Python. Depending on your use case, one of these methods may be more appropriate than the others.
Related Topics
- Print the following pattern using python
- Python Program to Check Leap Year
- Check if the first and last number of a list is the same | Python
- Number of occurrences of a substring in a string in Python
- Remove last element from list in Python
- How to Use Modulo Operator in Python
- Enumerate() in Python
- Writing to a File using Python's print() Function
- How to read csv file in Python
- Dictionary Comprehension in Python
- How to Convert List to String in Python
- How to convert int to string in Python
- Random Float numbers in Python