73

I have a string like this

>>> x="Alpha_beta_Gamma"
>>> words = [y for y in x.split('_')]
>>> words
['Alpha', 'beta', 'Gamma']

I want output saying X is non conformant as the the second element of the list words starts with a lower case and if the string x = "Alpha_Beta_Gamma" then it should print string is conformant

4
  • What about an x="Alpha_BEta_Gamma"? Sep 8, 2010 at 14:58
  • Good catch Alpha_BEta_Gamma is inavlid as it should be Alpha_Beta_Gamma
    – lisa
    Sep 8, 2010 at 15:14
  • @cris: it's completely irrelevant to the question and it certainly does not deserve py3k tag. Sep 8, 2010 at 17:10
  • @SilentGhost: You're right when you say that the py3k tag is irrelevant to the main problem, but I added it because of her problems with print. Sep 8, 2010 at 18:02

7 Answers 7

84

To test that all words start with an upper case use this:

print all(word[0].isupper() for word in words)
6
  • >>> x="Alpha_beta_Gamma" >>> words = [y for y in x.split('_')] >>> print all(word[0].isupper() for word in words) File "<stdin>", line 1 print all(word[0].isupper() for word in words) ^ SyntaxError: invalid syntax
    – lisa
    Sep 8, 2010 at 15:13
  • 1
    @lisa: You have a really old Python version (<2.4). Write print all([word[0].isupper() for word in words]) instead. Sep 8, 2010 at 15:18
  • 2
    @lisa: in Python3 use print(all...) because print is a function, not a statement. Sep 8, 2010 at 15:20
  • @THC4K: by the way old versions of Python didn't have all(). It is or was in a library provided by Google. Sep 8, 2010 at 15:21
  • @Cristian Ciupitu: Ah you are right, in Python3 it fails with a Syntax Error too. I just guessed the problem was the lack of generator expressions. Sep 8, 2010 at 15:24
65

Maybe you want str.istitle

>>> help(str.istitle)
Help on method_descriptor:

istitle(...)
    S.istitle() -> bool

    Return True if S is a titlecased string and there is at least one
    character in S, i.e. uppercase characters may only follow uncased
    characters and lowercase characters only cased ones. Return False
    otherwise.

>>> "Alpha_beta_Gamma".istitle()
False
>>> "Alpha_Beta_Gamma".istitle()
True
>>> "Alpha_Beta_GAmma".istitle()
False
2
  • Well hmm i have to mark right all though all the answers here were correct based on the original requirement i had given.
    – lisa
    Sep 8, 2010 at 15:27
  • Well hmm i have to mark your answer as right allthough all the answers here were correct based on the original requirement i had given.
    – lisa
    Sep 9, 2010 at 0:56
5
x="Alpha_beta_Gamma"
is_uppercase_letter = True in map(lambda l: l.isupper(), x)
print is_uppercase_letter
>>>>True

So you can write it in 1 string

2
  • I think you got it backwards -- OP seems to want it to print true if and only if the first letter (and no other) of each word is uppercase, and with yours, it prints true even if the conditions aren't met.
    – anon
    Apr 1, 2015 at 14:29
  • Also, that's weird python, looks more like java than py... A list comprehension would be far more pythonic. Mar 30, 2019 at 18:17
2
words = x.split("_")
for word in words:
    if word[0] == word[0].upper() and word[1:] == word[1:].lower():
        print word, "is conformant"
    else:
        print word, "is non conformant"
4
  • 1
    A part of the code is inefficient. You can replace it with word[0].isupper(). Sep 8, 2010 at 15:03
  • 1
    And the other part with word[1:].islower() Sep 8, 2010 at 15:05
  • 4
    @lisa: print is a function in Python 3. Please don't literally type the code here without thinking first and changing Python 2 things (like print statement) to Python 3 things like a print function. Also please don't say "giving me an error". Please provide the specific error.
    – S.Lott
    Sep 8, 2010 at 15:30
  • @S.Lott Thanks for guiding me so whats the expected norm of posting a error here are the specific errors posted in the comment section
    – lisa
    Sep 9, 2010 at 6:39
2

You can use this regex:

^[A-Z][a-z]*(?:_[A-Z][a-z]*)*$

Sample code:

import re

strings = ["Alpha_beta_Gamma", "Alpha_Beta_Gamma"]
pattern = r'^[A-Z][a-z]*(?:_[A-Z][a-z]*)*$'

for s in strings:
    if re.match(pattern, s):
        print s + " conforms"
    else:
        print s + " doesn't conform"

As seen on codepad

1
  • 1
    +1 but it might fail for non-English characters, e.g. "Ălălalt". Sep 8, 2010 at 15:15
2

You can use this code:

def is_valid(string):
    words = string.split('_')
    for word in words:
        if not word.istitle():
            return False, word
    return True, words
x="Alpha_beta_Gamma"
assert is_valid(x)==(False,'beta')
x="Alpha_Beta_Gamma"
assert is_valid(x)==(True,['Alpha', 'Beta', 'Gamma'])

This way you know if is valid and what word is wrong

0
-1

Use list(str) to break into chars then import string and use string.ascii_uppercase to compare against.

Check the string module: http://docs.python.org/library/string.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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