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?

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.