Python:以任何字母字符开头
如何使用startswith函数来匹配任何字母字符[a-zA-Z]。例如我想这样做:
if line.startswith(ALPHA):
Do Something
How can I use the startswith function to match any alpha character [a-zA-Z]. For example I would like to do this:
if line.startswith(ALPHA):
Do Something
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您还想匹配非 ASCII 字母,可以使用
str .isalpha
:If you want to match non-ASCII letters as well, you can use
str.isalpha
:您可以将元组传递给
startswiths()
(在 Python 2.5+ 中)以匹配其任何元素:当然,对于这个简单的情况,可以使用正则表达式测试或
in
运算符将更具可读性。You can pass a tuple to
startswiths()
(in Python 2.5+) to match any of its elements:Of course, for this simple case, a regex test or the
in
operator would be more readable.一个简单的解决方案是使用 python regex 模块:
An easy solution would be to use the python regex module:
这可能是最有效的方法:
This is probably the most efficient method:
如果你不关心字符串前面的空格,
if you don't care about blanks in front of the string,