Hi
How do you find out the first letter or number of string in a list?

I have a list and I want to find whether any of the strings in the list start with a capital or little C.

The code I have is:

def isCMD(self):
	for word in msgstrlist:
	    if msgstrlist[0][0] == 'C':
		print word
	else:
	    print "Not CMD message

I'm having a problem as it is only looking at the first word in the list. How can I change it so that it looks at all words?

Dani AI

Generated

Good question, . Two small tweaks make this robust and Pythonic: grab the first non-space character safely, and do case-insensitive matching. Using s.lstrip()[:1] avoids IndexError on empty strings, and casefold() is safer than upper() for case-insensitive checks. Also, prefer returning results instead of printing inside the loop so your code is easier to test. Building on and , here is a clean pattern:

def words_starting_with_c(items):
    # returns all strings that start with C or c (ignoring leading spaces)
    return [s for s in items if s.lstrip()[:1].casefold() == 'c']

To get the strings that do NOT start with C, R, or I, use a set for O(1) membership and the same first-character trick:

def not_starting_with(items, letters):
    blocked = {ch.casefold() for ch in letters}  # e.g., {'c', 'r', 'i'}
    out = []
    for s in items:
        first = s.lstrip()[:1].casefold()
        if first and first not in blocked:
            out.append(s)
    return out

# usage:
others = not_starting_with(msgstrlist, 'CRI')

Tips:

  • If you only need a yes/no, any(s.lstrip()[:1].casefold() == 'c' for s in msgstrlist) is concise.
  • Decide how to handle leading punctuation or digits. The lstrip() above ignores whitespace only; switch to lstrip(string.punctuation + string.whitespace) if you want to skip punctuation too.
  • Drop the self parameter unless this lives inside a class.

Recommended Answers

All 5 Replies

You are almost there:

def isCMD(msgstrlist):
    for word in msgstrlist:
        if word[0].upper() == 'C':
            print word
        else:
            print "Not CMD message"

msgstrlist = ['Ox', 'Dog', 'Cow', 'mouse', 'cat']

isCMD(msgstrlist)

"""
my result -->
Not CMD message
Not CMD message
Cow
Not CMD message
cat
"""

Note:
Looks like you are using a mix of tabs and spaces for indentations in your code, very bad habit!

Thanks for that. I am now trying to find the strings that don't start with the letters C, R or I. My code is:

def isOTHER(self):
	for word in msgstrlist:
	    if word[0].upper() not 'C' or 'R' or 'I'
		print "Other message has been found"
	    else:
		print "No other messages were found"

but it doesnt seem to be working. I have tried using not and or truth tests but not sure if I'm doing it right.

try this...

badletters = ['C', 'R', 'I']

# later on in your program...

if word[0].upper() not in badletters:
    print "Other message has been found"
else:
    print "No other messages were found"

please help me to develop a program in C for the given problems?
1. Read the string and print the first letters of word?
2. Read the array elements and find the greater and smallest after sorting it?
3. Read the string and print the digits and vowels ?

please help me to develop a program in C for the given problems?
1. Read the string and print the first letters of word?
2. Read the array elements and find the greater and smallest after sorting it?
3. Read the string and print the digits and vowels ?

You are in the wrong forum. Many of us went away from low level C to high level Python to solve such problems easily.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.