Create Python list split on spaces -


this question has answer here:

in python, how take full_name , create variable called name_list holds name list, split on spaces? know how create name_list = list(full_name) how split on spaces in 1 line?

var full_name = "my name" 

use str.split:

string.split(s[, sep[, maxsplit]])

return list of words of string s. if optional second argument sep absent or none, words separated arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). if second argument sep present , not none, specifies string used word separator. returned list have 1 more item number of non-overlapping occurrences of separator in string. if maxsplit given, @ maxsplit number of splits occur, , remainder of string returned final element of list (thus, list have @ maxsplit+1 elements). if maxsplit not specified or -1, there no limit on number of splits (all possible splits made).

full_name = "my name".split() 

it return list:

in [1]: full_name = "my name".split()  in [2]: full_name out[2]: ['my', 'name'] 

to assign first , last names variables can unpack:

in [3]: full_name = "my name"  in [4]: first, last = full_name.split()  in [5]: first out[5]: 'my'  in [6]: last out[6]: 'name' 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -