Reputation: 323
I am using Python 2 to make an app that counts the string words number and letters number.
def string_length(aStr):
number_of_words = len(aStr.split())
number_of_letters = len(aStr)
return number_of_words, number_of_letters
print string_length("my name is ahmed") #Returns (4, 16)
How can I turn (4, 16) to:
4
16
Upvotes: 1
Views: 43
Reputation: 184
Since the string_length
function is returning a tuple, you have to unpack them before printing.
def string_length(aStr):
number_of_words = len(aStr.split())
number_of_letters = len(aStr)
return number_of_words, number_of_letters
# Unpacking the tuple
words, letters = string_length('my name is ahmed')
print words
print letters
Upvotes: 1
Reputation: 61
call the function like
number_of_words, number_of_letters = string_length("Whatever string")
And then print the values
print number_of_words
print number_of_letters
Upvotes: 1