分析字符串输入,直到在Python上达到某个字母为止 [英] Analyzing string input until it reaches a certain letter on Python

查看:107
本文介绍了分析字符串输入,直到在Python上达到某个字母为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试编写程序的特定部分时,我需要帮助.这个想法是,一个人将输入一堆乱码,程序将读取它直到达到!".(感叹号),例如:

I need help in trying to write a certain part of a program. The idea is that a person would input a bunch of gibberish and the program will read it till it reaches an "!" (exclamation mark) so for example:

input("Type something: ")

人员类型:wolfdo65gtornado!salmontiger223

如果我要求程序打印输入,则仅应打印 wolfdo65gtornado ,并在到达!"时切断所有内容.该程序的其余部分正在分析和计数字母,但是我已经知道该怎么做.我只需要第一部分的帮助.我一直在尝试浏览这本书,但似乎我遗漏了一些东西.

If I ask the program to print the input it should only print wolfdo65gtornado and cut anything once it reaches the "!" The rest of the program is analyzing and counting the letters, but those part I already know how to do. I just need help with the first part. I been trying to look through the book but it seems I'm missing something.

我在想,也许利用 for 循环,然后对其施加限制,但我不知道如何分析随机插补字符串输入的某个字符,然后得到摆脱剩下的.

I'm thinking, maybe utilizing a for loop and then placing restriction on it but I can't figure out how to make the random imputed string input be analyzed for a certain character and then get rid of the rest.

如果您能提供帮助,我将不胜感激.谢谢!

If you could help, I'll truly appreciate it. Thanks!

推荐答案

内置的 str.partition() 方法将为您完成此操作.与 str.split() 不同,它不会打扰将其余的 str 切成不同的 str s.

The built-in str.partition() method will do this for you. Unlike str.split() it won't bother to cut the rest of the str into different strs.

text = raw_input("Type something:")
left_text = text.partition("!")[0]

这篇关于分析字符串输入,直到在Python上达到某个字母为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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