Analyzing string input until it reaches a certain letter on Python

112,038

Solution 1

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')

Solution 2

>>> 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 :("

Solution 3

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'

Solution 4

Try this:

s = "wolfdo65gtornado!salmontiger223"
m = s.index('!')
l = s[:m]

Solution 5

To explain accepted answer.

Splitting

partition() function splits string in list with 3 elements:

mystring = "123splitABC"
x = mystring.partition("split")
print(x)

will give:

('123', 'split', 'ABC')

Access them like list elements:

print (x[0]) ==> 123

print (x[1]) ==> split

print (x[2]) ==> ABC

Share:
112,038
anu_clarset
Author by

anu_clarset

Updated on July 10, 2022

Comments

  • anu_clarset
    anu_clarset almost 2 years

    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!