| | |
Finding the first letter of each string in a list
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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?
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?
You are almost there:
Note:
Looks like you are using a mix of tabs and spaces for indentations in your code, very bad habit!
python Syntax (Toggle Plain Text)
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 """
Looks like you are using a mix of tabs and spaces for indentations in your code, very bad habit!
Last edited by sneekula; Jan 8th, 2009 at 10:50 am.
No one died when Clinton lied.
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:
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.
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"
•
•
Join Date: Nov 2007
Posts: 148
Reputation:
Solved Threads: 32
try this...
python Syntax (Toggle Plain Text)
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"
Last edited by mn_kthompson; Jan 8th, 2009 at 11:51 am.
0
#6 Oct 12th, 2009
You are in the wrong forum. Many of us went away from low level C to high level Python to solve such problems easily.
Should you find Irony, you can keep her!
![]() |
Similar Threads
- txt files in pascal (Pascal and Delphi)
- Link-listed addressbook (C)
Other Threads in the Python Forum
- Previous Thread: GTK make a function wait for input from a button widget
- Next Thread: Word counting from file
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






