chevron_left
933 points
8 4 2

Problem:

Hello, kodlogs,

I am new to python programming language, many of my friends told me that Python is a better programming language for beginners. It is easy to pick up. I was convinced by their words and started learning Python. As days are passing I am discovering new things and it keeps attracting me. Well, today I was thinking to write a program that will remove a first character from a string. So I wrote a program below

x = "asdfghjk"

print(x[0])

I am wondering is there any other way to achieve this more efficiently!

Solution:

I have got your issue you to remove the python string datatype of the first characters in the most efficient way first of all if you want we must know that in python string datatype in immutable to any changes is not possible but if we are able to do the some changed you will end up creating the new object of it. and coming to your question if you want to get the first character way is perfect and efficient but if you want to remove it then you can use the slicing below is how can you use it.

strObj = "This is a sample string"
# Slice string to remove first character
strObj = strObj[1 : : ]
print('Modified  : ' , strObj)

Output:

Modified  :  his is a sample string

I hope this is going to help you, Happy coding!!

More Posts

Remove first and last character from string python Naymul01 - 3 days
How to get the first character of a string in python? Naymul01 - Oct 19
Remove first character from string c++ zeeshan - Jul 19, 2020
How to remove the first character from the string in C++? Lalit Kumar - Jun 19, 2020
Python remove spaces from string Naymul01 - Oct 15
How to remove duplicate characters from string in python amna - May 19
HOW TO REMOVE SUBSTRING FROM STRING IN PYTHON jaseera - Oct 23, 2020
HOW TO REMOVE A SUBSTRING FROM STRING IN PYTHON - Oct 23, 2020
How to remove a character from a string python Naymul01 - Oct 14
Python get first 4 characters of string Naymul01 - Oct 17