我正在使用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
答案 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