When i just make a variable,i get this. please see below
a = 'a d c'
a
'a d c'
a.split()
['a', 'd', 'c']
When i make a function, i get this. please see below
def x():
a = 'abc ash avs'
a.split()
print a
x()
abc ash avs
When i just make a variable,i get this. please see below
a = 'a d c'
a
'a d c'
a.split()
['a', 'd', 'c']
When i make a function, i get this. please see below
def x():
a = 'abc ash avs'
a.split()
print a
x()
abc ash avs
split()
returns a list of the words in the string you pass it. You are not updating the variable a
when calling it in your function.
def x():
a = 'abc ash avs'
a = a.split()
print a
You're not modifying the a
string itself since Python strings are immutable. Because you're in the interpreter it will print what a function returns. That's why you get the list printed on screen there. But the x
function doesn't return the split list, merely prints a
.
You'll see your expected result if you switch to this:
def x():
a = 'abc ash avs'
print a.split()
And you can see what I mean about a
not being modified with this:
a = 'a d c'
a
'a d c'
a.split()
['a', 'd', 'c']
a
'a d c'
The method split()
split your string based on whitespace when you don't pass a specific argument to it. So in your function it split 'abc ash avs'
based on spaces.
If you want the characters you can use list
function.
>>> a = 'abc ash avs'
>>> list(a)
['a', 'b', 'c', ' ', 'a', 's', 'h', ' ', 'a', 'v', 's']
And if you want alphabetical characters you can use a list comprehension to get them :
>>> [i for i in a if i.isalpha()]
['a', 'b', 'c', 'a', 's', 'h', 'a', 'v', 's']