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
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.
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.
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 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.
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 ()
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With