user3014479
user3014479

Reputation: 61

String tokenization python

I wanted to split a string in python.

s= "ot.jpg/n"

I used str.split() but it gives me ['ot.jpg']. I want to get ot.jpg without brackets.

Upvotes: 0

Views: 610

Answers (3)

justengel
justengel

Reputation: 6320

sounds like you want replace.

s= "ot.jpg/n".replace("/n", "")
"ot.jpg"

Upvotes: 0

bgporter
bgporter

Reputation: 36504

The return value of the split() method is always a list -- in this case, it's given you a single-element list theList = ['ot.jpg']. Like any list, you get what you want out of it by indexing it:

myString = theList[0]

Upvotes: 0

nmichaels
nmichaels

Reputation: 50941

You want to use str.strip() to get rid of the newline. If you need to use split, it returns a list. To get the nth item from a list, index the list: ['foo', 'bar'][n].

Incidentally, naming your string str is a bad idea, since it shadows the built-in str function.

Upvotes: 1

Related Questions