如何在Python中打印以特定字符开头的字符串? [英] How do I print strings that begin with a certain character in Python?

查看:711
本文介绍了如何在Python中打印以特定字符开头的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果字符的大小写和字符串的第一个字母在字符串的第一个字母不应该对是否有所影响,我怎么能打印以某个字符开头的列表中的所有项目(以字符串列表和字符作为参数)字符串是否打印?



我尝试过:



How could I print all items of a list that begin with a certain character (taking a list of strings and a character as parameters) when capitalization of the character and the first letter of the string should not make a difference in whether the string is printed or not?

What I have tried:

def partition (strings, char):
    if strings == []:
        print('')
    else:
        for string in strings:
            if string[0] == char:
                print(string)

推荐答案

将它们变成同一个案例比较之前:
turn them into the same case before comparing:
if string[0].upper() == char.upper():


这篇关于如何在Python中打印以特定字符开头的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆