Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting strings using a delimiter in python [closed]

Tags:

OK so I have a string that has this:

Dan|warrior|54 

I'm trying to make so I can use python and split it using | as the delimiter. Here's what I have so far:

#!/usr/bin/env python dan = 'dan|warrior|54' print dan.split('|') 

and that results into this:

['dan', 'warrior', '54'] 

I know it's incomplete but what do I have to do to finish it? Yes, I tried googling this problem... but it's not happening. :(

I want so that I can choose specifically which one from the delimiter so if I was dan.split('|')[1] .. it would pick warrior. See my point?

like image 925
test Avatar asked Jan 17 '11 19:01

test


People also ask

How do you split a string into parts based on a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.

Can I split a string by two delimiters Python?

Python has a built-in method you can apply to string, called . split() , which allows you to split a string by a certain delimiter.


1 Answers

So, your input is 'dan|warrior|54' and you want "warrior". You do this like so:

>>> dan = 'dan|warrior|54' >>> dan.split('|')[1] "warrior" 
like image 82
Lennart Regebro Avatar answered Sep 20 '22 15:09

Lennart Regebro