-4

I know that in Python you can use the array selector to retrieve a certain part of a string, ie me.name[10:] to get just the last 10 characters.

but how would you retrieve just the part of a string after an underscore ie _ using a single expression? For example if my string is "stringcharThatChange_myname"

How would I extract just 'myname' ? I'm confined to using Python 3.5.1

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Bachalo
  • 6,965
  • 27
  • 95
  • 189

2 Answers2

3

You could use split.

test_string = "stringcharThatChange_myname"
print(test_string.split('_')[1]) # myname
cmxu
  • 954
  • 5
  • 13
0

Using split method.

"this will be excluded_this is kept".split('_')[1]

albestro
  • 121
  • 1
  • 4