w3resource

Python: Index number of all lower case letters in a given string


Indices of Lowercase Letters

Write a Python program to get the index number of all lower case letters in a given string.

Sample Solution-1:

Python Code:

# Define a function 'test' that returns the indices of lowercase letters in a given text.
def test(text):
   # Use a list comprehension to find indices of lowercase letters.
   return [x for x in range(len(text)) if text[x].islower()]

# Provide a sample text "Python".
text = "Python"
# Print the original text.
print("Original string:", text)
# Print the indices of all lowercase letters in the sample text using the 'test' function.
print("Indices of all lower case letters of the said string:\n", test(text))

# Provide another sample text "JavaScript".
text = "JavaScript"
# Print the original text.
print("\nOriginal string:", text)
# Print the indices of all lowercase letters in the sample text using the 'test' function.
print("Indices of all lower case letters of the said string:\n", test(text))

# Provide one more sample text "PHP".
text = "PHP"
# Print the original text.
print("\nOriginal string:", text)
# Print the indices of all lowercase letters in the sample text using the 'test' function.
print("Indices of all lower case letters of the said string:\n", test(text))

Sample Output:

Original string: Python
Indices of all lower case letters of the said string:
 [1, 2, 3, 4, 5]
Original string: JavaScript
Indices of all lower case letters of the said string:
 [1, 2, 3, 5, 6, 7, 8, 9]
Original string: PHP
Indices of all lower case letters of the said string:
 []

Explanation:

Here is a breakdown of the above Python code:

  • Test Function (test function):
    • The "test()" function is defined to return the indices of lowercase letters in a given text.
    • It uses a list comprehension to iterate through the text and find indices where each character is lowercase.

Flowchart:

Flowchart: Python - Index number of all lower case letters in a given string.

Sample Solution-2:

Python Code:

# Define a function 'test' that returns the indices of lowercase letters in a given text.
def test(text):
   # Use list comprehension with enumerate to find indices of lowercase letters.
   return [index for index, element in enumerate(text) if element.islower()]

# Provide a sample text "Python".
text = "Python"
# Print the original text.
print("Original string:", text)
# Print the indices of all lowercase letters in the sample text using the 'test' function.
print("Indices of all lower case letters of the said string:\n", test(text))

# Provide another sample text "JavaScript".
text = "JavaScript"
# Print the original text.
print("\nOriginal string:", text)
# Print the indices of all lowercase letters in the sample text using the 'test' function.
print("Indices of all lower case letters of the said string:\n", test(text))

# Provide one more sample text "PHP".
text = "PHP"
# Print the original text.
print("\nOriginal string:", text)
# Print the indices of all lowercase letters in the sample text using the 'test' function.
print("Indices of all lower case letters of the said string:\n", test(text))

Sample Output:

Original string: Python
Indices of all lower case letters of the said string:
 [1, 2, 3, 4, 5]
Original string: JavaScript
Indices of all lower case letters of the said string:
 [1, 2, 3, 5, 6, 7, 8, 9]
Original string: PHP
Indices of all lower case letters of the said string:
 []

Explanation:

Here is a breakdown of the above Python code:

  • Test Function (test function):
    • The "test()" function is defined to return the indices of lowercase letters in a given text.
    • It uses a list comprehension to iterate through the text and find indices where each character is lowercase.

Flowchart:

Flowchart: Python - Index number of all lower case letters in a given string.

For more Practice: Solve these Related Problems:

  • Write a Python program to find the indices of all lowercase letters in a string using enumerate.
  • Write a Python program to return a list of positions where characters in a string are lowercase.
  • Write a Python program to identify and list the index positions of all lowercase alphabetic characters in a given string.
  • Write a Python program to scan a string and output the indices of characters that are in lowercase.

Go to:


Previous: Write a Python program to remove all vowels from a given string.
Next: Write a Python program to check whether a given month and year contains a Monday 13th.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.