Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analyzing string input until it reaches a certain letter on 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: ")

Person types: wolfdo65gtornado!salmontiger223

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.

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!

like image 727
anu_clarset Avatar asked Nov 17 '11 04:11

anu_clarset


People also ask

How do you check if a the input is a specific letter in Python?

You can use num. isnumeric() function that will return You "True" if input is number and "False" if input is not number. You can also use try/catch: try: val = int(num) except ValueError: print("Not an int!")

How do you extract text before a character in Python?

Python Substring Before Character You can extract a substring from a string before a specific character using the rpartition() method. rpartition() method partitions the given string based on the last occurrence of the delimiter and it generates tuples that contain three elements where.

How do you search for a specific text in a string in Python?

String find() in Python Just call the method on the string object to search for a string, like so: obj. find(“search”). The find() method searches for a query string and returns the character position if found. If the string is not found, it returns -1.


4 Answers

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]

Explanation

str.partition() returns a 3-tuple containing the beginning, separator, and end of the string. The [0] gets the first item which is all you want in this case. Eg.:

"wolfdo65gtornado!salmontiger223".partition("!")

returns

('wolfdo65gtornado', '!', 'salmontiger223')
like image 122
Michael Hoffman Avatar answered Oct 18 '22 16:10

Michael Hoffman


>>> s = "wolfdo65gtornado!salmontiger223"
>>> s.split('!')[0]
'wolfdo65gtornado'
>>> s = "wolfdo65gtornadosalmontiger223"
>>> s.split('!')[0]
'wolfdo65gtornadosalmontiger223'

if it doesnt encounter a "!" character, it will just grab the entire text though. if you would like to output an error if it doesn't match any "!" you can just do like this:

s = "something!something"
if "!" in s:
  print "there is a '!' character in the context"
else:
  print "blah, you aren't using it right :("
like image 33
Ole Avatar answered Oct 18 '22 15:10

Ole


You want itertools.takewhile().

>>> s = "wolfdo65gtornado!salmontiger223"
>>> '-'.join(itertools.takewhile(lambda x: x != '!', s))
'w-o-l-f-d-o-6-5-g-t-o-r-n-a-d-o'



>>> s = "wolfdo65gtornado!salmontiger223!cvhegjkh54bgve8r7tg"
>>> i = iter(s)
>>> '-'.join(itertools.takewhile(lambda x: x != '!', i))
'w-o-l-f-d-o-6-5-g-t-o-r-n-a-d-o'
>>> '-'.join(itertools.takewhile(lambda x: x != '!', i))
's-a-l-m-o-n-t-i-g-e-r-2-2-3'
>>> '-'.join(itertools.takewhile(lambda x: x != '!', i))
'c-v-h-e-g-j-k-h-5-4-b-g-v-e-8-r-7-t-g'
like image 42
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 17:10

Ignacio Vazquez-Abrams


Try this:

s = "wolfdo65gtornado!salmontiger223"
m = s.index('!')
l = s[:m]
like image 41
Mohammad Rahmani Avatar answered Oct 18 '22 16:10

Mohammad Rahmani