如何断开函数调用的值

时间:2018-04-01 21:12:10

标签: python

我正在使用Python 2制作一个计算字符串字数和字母数的应用程序。

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)

如何将(4,16)转为:

4

16

2 个答案:

答案 0 :(得分:1)

调用函数

number_of_words, number_of_letters = string_length("Whatever string")

然后打印值

 print number_of_words
 print number_of_letters

答案 1 :(得分:1)

由于string_length函数返回tuple,您必须在打印前解包

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