1

Let's say I have a string, and I want to check a lot of conditions on this string. For example:

  1. It is of size x
  2. Has no white spaces
  3. The last letter is a numeric character

All of the conditions from 1-3 It can be done in a regular standard way in a function (if this and that etc')

But... How would I do that in one line in a good python style way?

NullUserException
85.8k31 gold badges212 silver badges239 bronze badges
asked May 23, 2012 at 19:11
5
  • 3
    There are certainly other ways to do it, but I don't think anything will be as clear as the "if this and that etc." Commented May 23, 2012 at 19:14
  • Just use a regex, \S{x-1}\d$, (replacing x-1 with the actual number) Commented May 23, 2012 at 19:15
  • Now you have 2 problems. Commented May 23, 2012 at 19:20
  • @mayhewr Do enlighten us. Is there a justification to this vitriol against regexes besides "I don't understand them"? Commented May 23, 2012 at 19:22
  • 1
    To me, regex, while totally awesome, powerful, and admittedly appropriate in OP's situation, is not the "pythonic" way of solving this problem, which is what OP asked for. The "pythonic" way is what @mgilson said IMO. Also, I think it's funny. Commented May 23, 2012 at 19:37

4 Answers 4

7

You can use a simple if condition with any:

s='fdsfgsgsfds9'
if len(s)==7 and not any(c.isspace() for c in s) and s[-1].isdigit():
 pass
answered May 23, 2012 at 19:24
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the non-regex solution that is far more readable for all users, especially those not regex-inclined.
I would write this as if len(s) == 7 and not any(c.isspace() for c in s) and s[-1].isdigit(): somewhat easier to read, I think.
@DSM thanks for teaching me about any(),solution edited. :)
5

It may be more complicated than it's worth, but you can check all of those conditions with a regular expression.

For example, if the size you wanted was 8 characters, you could use the following to check all three conditions:

if re.match(r'\S{7}\d$', text):
 print 'all conditions match'
sblom
27.5k4 gold badges74 silver badges96 bronze badges
answered May 23, 2012 at 19:15

Comments

4

One way would be to use a regular expression (assuming an x of 10):

if re.match(r"\S{10}(?<=\d)$", mystring):
 # Success!
answered May 23, 2012 at 19:16

5 Comments

@PaulBeckingham: No, it isn't. You do know about lookaround assertions?
10 + the digit, so your example is for 11, not 10.
@PaulBeckingham That's a zero-width assertion.
Why not change this to use a format string and use any number. This would be valid, right? "\\S{%d}(?<=\\d)$" % x (not sure if you can do this with raw string)
@Jeff: Good idea. And yes, it also works with a raw string: re.match(r"\S{%d}(?<=\d)$" % x, mystring)
1

Try something like

import re
def test(s):
 return len(s)>=x and re.match("^\S*\d$", s)

This will test whether the string has length of at least x and that it is a sequence of non-space-characters followed by a digit character at the end.

answered May 23, 2012 at 19:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.