Marcello B.
Marcello B.

Reputation: 4440

How do I delete the first part of a string in python

Hello all I have a program that is dealing with accounts and their associated names for example my username is user1234 and my real name is Bob Realname the data is store as "user1224 : Bob Realname" so far my program strips it down to user1234 Bob Realname what I would like to do is get rid of the part that says user1234 and just get the result Bob Realname.

Upvotes: 2

Views: 264

Answers (2)

jonafato
jonafato

Reputation: 1605

Try using split and strip.

>>> 'user1224 : Bob Realname'.split(':')[1].strip()
'Bob Realname'

Upvotes: 3

mgilson
mgilson

Reputation: 309929

I would split the original string on ':' and then take the second half:

original.split(':')[1]

e.g:

>>> original = "user1224 : Bob Realname"
>>> original.split(':')[1]
' Bob Realname'
>>> original.split(':')[1].strip()  # remove leading whitespace.
'Bob Realname'

Upvotes: 5

Related Questions