Iterating Over Strings Part 1
Part of the Fundamentals section of Coddy's Python journey — lesson 62 of 77.
Similar to lists, you can iterate over strings:Output:Using index:Output:You can also modify strings using methods. For example, to convert a character to lowercase, use the Output:
text = "Hey"
for char in text:
print(char)H
e
yfor i in range(len(text)):
print(f"position {i}: {text[i]}")position 0: H
position 1: e
position 2: y.lower() method:char = "A"
print(char.lower())aChallenge
EasyCreate a program that receives a string as input, and it prints how many times the character p (or P) is in the string.
Some chars might be uppercase, use char.lower() to convert it to lowercase.
Try it yourself
text = input()
# Write your code below
# Hint: To ignore case, you can convert characters to lowercase using the .lower() method.
# Example: char.lower()This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 48Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionRecap - Validation FunctionDefault Values12Iterating Over Sequences
Iterating Over ElementsThe Enumerate FunctionIterating Over Strings Part 1Iterating Over Strings Part 2