Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to split a string and unpack into variables?

Simple question: given a string

string = "Word1 Word2 Word3 ... WordN"

is there a pythonic way to do this?

firstWord = string.split(" ")[0]
otherWords = string.split(" ")[1:]

Like an unpacking or something?

Thank you

like image 594
Thomas Kowalski Avatar asked May 25 '17 08:05

Thomas Kowalski


People also ask

How do I split a string into separate variables?

Use the Split() Function to Split a String Into Separate Variables in PowerShell. The Split() is a built-in function used to split a string in PowerShell. We can store the result of the Split() function into multiple variables.

How do you split a string into variables in Python?

To split string variables at each whitespace, we can use the split() function with no arguments. The syntax for split() function is split(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split.

How do I unpack a string in Python?

Method : Using format() + * operator + values() The combination of above functions can be used to solve this problem. In this, we use format to map required value with braces in string. The * operator is used to unpack and assign. The values are extracted using values().

How to split string variables in Python?

How to split string variables in Python Author: Aditya Raj Last Updated: May 14, 2021 There may be many situations where we may need to split string variables. To split string variables in python we can use split()method, rsplit()method and splitlines()method with different arguments to the functions to accomplish different tasks.

How to unpack a string with multiple words in Python?

Since Python 3 and PEP 3132, you can use extended unpacking. This way, you can unpack arbitrary string containing any number of words. The first will be stored into the variable first, and the others will belong to the list (possibly empty) others. first, *others = string.split ()

How to recombine a string that has been split in Python?

If you do specify maxsplit and there are an adequate number of delimiting pieces of text in the string, the output will have a length of maxsplit+1. Recombining a string that has already been split in Python can be done via string concatenation. Python split () only works on string variables.

Is it possible to split string into variables in one line?

Possible Duplicate: Python Split String Is possible to directly split string into variables in one line, instead of using two lines. I'm sure that split would have two elements.


2 Answers

Since Python 3 and PEP 3132, you can use extended unpacking.

This way, you can unpack arbitrary string containing any number of words. The first will be stored into the variable first, and the others will belong to the list (possibly empty) others.

first, *others = string.split()

Also, note that default delimiter for .split() is a space, so you do not need to specify it explicitly.

like image 114
Delgan Avatar answered Nov 14 '22 22:11

Delgan


From Extended Iterable Unpacking.

Many algorithms require splitting a sequence in a "first, rest" pair, if you're using Python2.x, you need to try this:

seq = string.split()
first, rest = seq[0], seq[1:]

and it is replaced by the cleaner and probably more efficient in Python3.x:

first, *rest = seq

For more complex unpacking patterns, the new syntax looks even cleaner, and the clumsy index handling is not necessary anymore.

like image 21
McGrady Avatar answered Nov 14 '22 21:11

McGrady