I have a string such as

"this is a really long string that is completely pointless. But hey who cares!

and I was wondering if there was a way to cut the first X characters off and return the res of the string

Recommended Answers

All 3 Replies

Python string are immutable so delete it wont work.
You can replace it first character with nothing ''.

>>> s = "this is a really long string that is completely pointless. But hey who cares!"
>>> s.replace(s[0], '')
'his is a really long sring ha is compleely poinless. Bu hey who cares!'

Another way is make it a list and the do stuff you need,and take it back into a string again.

>>> s = "this is a really long string that is completely pointless. But hey who cares!"
>>> s = list(s)  #Make it a list
>>> del(s[0])    #Delete first character
>>> ''.join(s)   #Take it back to a string
'his is a really long string that is completely pointless. But hey who cares!'

Here you go duddy :)

dd= "this is a really long string that is completely pointless. But hey who cares!".split(" ")
dd[0]=''
print " ".join(dd)
##out
is a really long string that is completely pointless. But hey who cares!

Common idiom is to construct fresh string using slicing. That is assigned to old variable if it is not needed anymore. This can be further developed by doing the string generation in generator expression we can pass to join method producing suitably joined new string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.