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

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
lisa
  • 753
  • 1
  • 6
  • 6

7 Answers7

84

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

print all(word[0].isupper() for word in words)
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
  • >>> x="Alpha_beta_Gamma" >>> words = [y for y in x.split('_')] >>> print all(word[0].isupper() for word in words) File "", line 1 print all(word[0].isupper() for word in words) ^ SyntaxError: invalid syntax – lisa Sep 08 '10 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. – Jochen Ritzel Sep 08 '10 at 15:18
  • 2
    @lisa: in Python3 use `print(all...)` because `print` is a function, not a statement. – Cristian Ciupitu Sep 08 '10 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. – Cristian Ciupitu Sep 08 '10 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. – Jochen Ritzel Sep 08 '10 at 15:24
  • Cristian well your answer by the way is brilliant to my original question – lisa Sep 08 '10 at 15:30
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
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
  • 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 08 '10 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 09 '10 at 00: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

mihoff
  • 59
  • 1
  • 1
  • 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. – Nic Apr 01 '15 at 14:29
  • Also, that's weird python, looks more like java than py... A list comprehension would be far more pythonic. – Fábio Dias Mar 30 '19 at 18:17
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

NullUserException
  • 83,810
  • 28
  • 209
  • 234
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"
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 1
    A part of the code is inefficient. You can replace it with `word[0].isupper()`. – Cristian Ciupitu Sep 08 '10 at 15:03
  • 1
    And the other part with `word[1:].islower()` – NullUserException Sep 08 '10 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 08 '10 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 09 '10 at 06:39
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

Rafael Sierra
  • 459
  • 1
  • 4
  • 14
-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

Drew Nichols
  • 735
  • 1
  • 6
  • 13