Reputation: 1
I want to create a Python program to with function that gives token('Hello, World!') == ['hello', 'world'], but my result gives ['hello,', 'world']. What is going on?
import string
def token(text):
list=[]
text = text.lower()
new_string = text.split()
for x in new_string:
list.append(x)
return list
token('Hello, World!')
Upvotes: 0
Views: 948
Reputation: 35
I think what you are trying to do is have text.split(','), so that way you split the text wrt ','. Also, you may want to indent the last line, else it will not be part of your function. Here is what I did:
import string
def token(text):
list=[]
text = text.lower()
new_string = text.split()
for x in new_string:
list.append(x)
return list
print(token('Hello,World!')) # ['hello,world!']
Upvotes: 0
Reputation: 5745
it looks like you had issue with you return statement indentation, but i can give you advice in a better way using list comprenesion, also its not a good practice to use the saved keyword list
:
def token(text):
return [x.strip() for x in text.lower().split(',')]
print(token("yOu, wElcome")) #==> ['you', 'welcome']
Upvotes: 1
Reputation: 2423
Try this instead :
def token(text):
list=[]
text = text.lower()
new_string = text.split(',')
for x in new_string:
list.append(x.strip())
return list
print(token("Hello, World!")) #==> ['hello', 'world!']
Upvotes: 1