Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So, I'm working on a script in Python 3, and I need something like this

control_input=input(prompt_029)
if only string_029 and int in control_input:
    #do something
else:
    #do something else

Basically, I am asking for code that would have condition like this:

if control_input == "[EXACT_string_here] [ANY_integer_here]"

How would the code look like in Python 3?

share|improve this question
1  
Use regular expressions. –  millimoose Mar 24 '13 at 16:35

2 Answers 2

up vote 2 down vote accepted

What you want is to do a regular expression match. Take a look at the re module.

>>> import re
>>> control_input="prompt_029"
>>> if re.match('^prompt_[0-9]+$',control_input):
...     print("Matches Format")
... else:
...     print("Doesn't Match Format")
... 
Matches Format

The regular expression ^prompt_[0-9]+$ matches the following:

^        # The start of the string 
prompt_  # The literal string 'prompt_'
[0-9]+   # One or more digit 
$        # The end of the string 

If the number must contain exactly three digits then you could use ^prompt_[0-9]{3}$ or for a maximum of three then try ^prompt_[0-9]{1,3}$.

share|improve this answer

Without regular expression

>>> myvar = raw_input("input: ")
input: string 1
>>> myvar
'string 1'
>>> string, integer = myvar.strip().split()
>>> "[EXACT_string_here] [ANY_integer_here]" == "{} {}".format(string, integer)
True
share|improve this answer
    
This doesn't really do what the OP asked for - the integer part in the input is variable, not constantly 1, and there's no straightforward way to modify your code to allow for that. –  millimoose Mar 24 '13 at 18:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.