挑战Python100题(4)

100+ Python challenging programming exercises 4

Question 31

Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.

Hints:

Use len() function to get the length of a string

定义一个可以接受两个字符串作为输入的函数,并在控制台中打印最大长度的字符串。如果两个字符串的长度相同,那么函数应该逐行打印所有字符串。

提示:

使用len()函数获取字符串的长度

Solution

def printValue(s1,s2):
    len1 = len(s1)
    len2 = len(s2)
    if len1>len2:
        print(s1)
    elif len2>len1:
        print(s2)
    else:
        print(s1)
        print(s2)

printValue("one","two")
printValue("one","three")

版权声明:本文为博主作者:Hann Yang原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/boysoft2002/article/details/135164237

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2024年1月3日
下一篇 2024年1月3日

相关推荐