Reputation: 61
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
Reputation: 6320
sounds like you want replace.
s= "ot.jpg/n".replace("/n", "")
"ot.jpg"
Upvotes: 0
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
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