How can I write the code so python
ONLY prints out the lower case letters of the string. In this case, it should exclude the P.
I tried this:
word = "Programming is fun!"
for letter in word:
if letter.lower[]:
print letter
But it doesn't solely print out the lowercase letters. How could I only get the lower-case characters out of the string instead of the whole string in lower-case.
asked Dec 24, 2015 at 2:03
Jason_SilvaJason_Silva
1193 silver badges12 bronze badges
2
You want letter.islower[]
[which tests], not letter.lower[]
[which converts].
If you want to print non-cased characters too, you'd check:
if letter.islower[] or not letter.isalpha[]:
answered Dec 24, 2015 at 2:05
ShadowRangerShadowRanger
131k12 gold badges168 silver badges244 bronze badges
Try using islower
instead :
letter.islower[]
answered Dec 24, 2015 at 2:05
astrosyamastrosyam
8474 silver badges15 bronze badges
Yours doesn't work because you've called .lower[]
, which is a method of the String class - it changes the
case of the letter in question, and results in True
.
There are likely many ways to obtain the result you want, the simplest I can think of being:
word = "Hello World!"
for letter in word:
if letter.islower[]:
print[letter]
Note that there is an equality test in mine, which is what yours is missing.
EDIT: As other answers pointed out, .islower[]
is a more succinct way of checking the case of the letter. Similarly, .isupper[]
would print
only the capital letters.
answered Dec 24, 2015 at 2:11
BlackbeardBlackbeard
3452 silver badges7 bronze badges
You could use
print filter[lambda c: c.islower[], word]
answered Dec 24, 2015 at 2:05
Frerich RaabeFrerich Raabe
87.5k18 gold badges112 silver badges206 bronze badges
What will actually answer to your question [as i can tell from provided
output] will be something like: import string
word="Programming is fun!"
filter[lambda c: c.islower[] or not[c.isupper[] and c in string.printable], word]
answered Dec 24, 2015 at 2:25
Sometimes, while working with strings, we are concerned about the case sensitivity of strings and might require getting just a specific case of character in a long string. Let’s discuss certain ways in which only lowercase letters can be extracted from a string.
Method #1: Using list comprehension + islower[]
List comprehension and islower function can be used to perform this particular task. The list comprehension is primarily used to iterate over the list and islower function checks for the lowercase characters.
Python3
test_str
=
"
GeeksForGeeKs & quot
print
[& quot
The original string
is
: & quot
+
str
[test_str]]
res
=
[char
for
char
in
test_str
if
char.islower[]]
print
[& quot
The lowercase characters
in
string are : & quot
+
str
[res]]
Output :
The original string is : GeeksForGeeKs The lowercase characters in string are : [‘e’, ‘e’, ‘k’, ‘s’, ‘o’, ‘r’, ‘e’, ‘e’, ‘s’]
Method #2: Using filter[] + lambda
Filter function along with lambda functionality can be used to perform this particular task. The filter function performs the specific selection of case characters and the lambda function is used for string traversal.
Python3
test_str
=
"
GeeksForGeeKs & quot
print
[& quot
The original string
is
: & quot
+
str
[test_str]]
res
=
list
[
filter
[
lambda
c: c.islower[], test_str]]
print
[& quot
The lowercase characters
in
string are : & quot
+
str
[res]]
Output :
The original string is : GeeksForGeeKs The lowercase characters in string are : [‘e’, ‘e’, ‘k’, ‘s’, ‘o’, ‘r’, ‘e’, ‘e’, ‘s’]
Method #3: Without using islower[] builtin method
Python3
test_str
=
"GeeksForGeeKs"
loweralphabets
=
"abcdefghijklmnopqrstuvwxyz"
print
[
"The original string is : "
+
str
[test_str]]
res
=
[]
for
i
in
test_str:
if
i
in
loweralphabets:
res.append[i]
print
[
"The lowercase characters in string are : "
+
str
[res]]
Output
The original string is : GeeksForGeeKs The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']
Method #4: Using ord[] method.The ASCII value of the lowercase alphabet is from 97 to 122.
Python3
test_str
=
"GeeksForGeeKs"
print
[
"The original string is : "
+
str
[test_str]]
res
=
[]
for
i
in
test_str:
if
ord
[i]
in
range
[
97
,
123
]:
res.append[i]
print
[
"The lowercase characters in string are : "
+
str
[res]]
Output
The original string is : GeeksForGeeKs The lowercase characters in string are : ['e', 'e', 'k', 's', 'o', 'r', 'e', 'e', 's']