0

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.

1
  • 3
    Did you maybe confuse "split" and "strip"? Commented Jan 30, 2014 at 15:30

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

0

sounds like you want replace.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.