1

I'm trying to create code that will take text input, eliminate all whitespace characters (left and right, not those between characters) and then validate if the string has following format: 'XXX XXX' (where X is letter or digit). If it doesn't (say that it has XXX-XXX or XXXXXX format) it would convert it to such format.

I know that I can use strip to remove whitespace from a string. I also know how to check the length of a string and how to convert it, but I don't know how to validate a string after using .strip().

2
  • 1
    You should use regular expressions (re). Commented Jun 13, 2018 at 16:14
  • 1
    Please provide code examples of what you've tried. As DyZ said, you can use regular expressions. Commented Jun 13, 2018 at 16:19

1 Answer 1

1

Use regular expressions.

>>> import re
>>> inp.strip() 
aB8-9uG
>>> match = re.match(r"([0-9a-zA-Z]{3})(?:\-| |)([0-9a-zA-Z]{3})", _)
>>> match
<_sre.SRE_Match object; span=(0, 7), match='aB8-9uG'>
>>> match.group(1) + " " + match.group(2)
aB8 9uG
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.