在Python 3中,如何确定字符串中是否包含前缀? [英] In Python 3, how do I determine if a prefix is in a string?

查看:391
本文介绍了在Python 3中,如何确定字符串中是否包含前缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要递归地编写一个函数,该函数确定前缀(例如"f")是否为字符串的前缀.如果是前缀,则需要返回True,如果不是前缀,则需要返回False.我该怎么办?

I need to recursively write a function that determines if a prefix, for example 'f', is the prefix of a string. If it is the prefix, it needs to return True, and if it is not the prefix, it needs to return False. How do I do this?

推荐答案

使用 startswith 方法:

if(string.startswith(prefix)) :
    do something

这篇关于在Python 3中,如何确定字符串中是否包含前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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