0

Okay, I'm not really getting the answers I was looking for so I'll try rewording this. I need to know if there is a function that detects the lowercase letters in a string. Say I started writing this program:

s = input('Type a word')

Would there be a function that lets me detect a lowercase letter within the string s? Possibly ending up with assigning those letters to a different variable, or just printing the lowercase letters or number of lowercase letters.

While those would be what I would like to do with it I'm most interested in how to detect the presence of lowercase letters. The simplest methods would be welcome, I am only in an introductory python course so my teacher wouldn't want to see complex solutions when I take my midterm. Thanks for the help!

JustaGuy313
  • 501
  • 2
  • 6
  • 10

2 Answers2

1

Using a regular expression is probably the simplest way:

# Python 3 version

import re

txt = 'MarMaLadE'

# Replace uppercase with "" - an empty string
ltxt,n = re.subn(r'[A-Z]', "", txt) 

print("lower case letters:"," ".join(set(ltxt)))
print("number of lc letters:", n)

The use of set removes duplicate lowercase letters, you might not want that. You might also want to consider which characters are uppercase, is A-Z sufficient? See also the re.LOCALE flag.

cdarke
  • 37,606
  • 5
  • 69
  • 77
0

Hope this helps, keep in mind that python is wary of indentation. This is not a very good solution but it works.

def doWhatYouWantToDo(input):
 newString = ""
 alphabet = "abcdefghijklmnopqrstuvwxyz"
 for letter in input:
  if letter in alphabet:
   newString += letter
 return newString

yourString = 'MarMaLadE'
print doWhatYouWantToDo(yourString)
print len(doWhatYouWantToDo(yourString)
godzilla3000
  • 238
  • 3
  • 20