-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
  • 963,270
  • 265
  • 3,804
  • 3,187
Bachalo
  • 6,497
  • 25
  • 88
  • 184

2 Answers2

0

You could use split.

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

Using split method.

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

albestro
  • 121
  • 1
  • 4