Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Special Methods
Flow control
Looping through a String
Looping through a string in Python means iterating over each character in the string one by one, using a loop. Since a string is a sequence of characters, you can treat it like any other iterable (such as a list or tuple) in a for loop.
1. Using a for loop to iterate over a String:
1. Using a for loop to iterate over a String:
word = "Python" for letter in word: print(letter)
- The string Python is assigned to the variable word.
- The for loop iterates over the string character by character.
- On each iteration, the variable letter takes the value of the current character from the string.
- The print(letter) statement outputs the character for each iteration.
Output:
P y t h o n
2. Using a while loop to iterate over a String:
You can also loop through a string using a while loop by manually tracking the index of each character.
Example:
word = "Python" i = 0 while i < len(word): print(word[i]) i += 1
- The variable i is initialized to 0, which will be used as the index.
- The while loop continues as long as i is less than the length of the string (len(word)).
- The string's character at index i is printed with word[i].
- After each iteration, i is incremented by 1 to move to the next character.
Output:
P y t h o n
3. Looping through a string with range():
You can use range() in combination with the length of the string to iterate through its characters by their index.
Example:
word = "Python" for i in range(len(word)): print(word[I])
- range(len(word)) generates a range of numbers from 0 to the length of the string minus one.
- On each iteration, i takes the value of the current index, and word[i] accesses the character at that index.
Output:
P y t h o n
4. Use cases:
Checking characters in a string: While looping through a string, you can use conditions to check or manipulate characters.
Example:
sentence = "Hello World" for char in sentence: if char == 'o': print("Found 'o'") else: print(char)
- The loop checks each character in the string sentence.
- If the character is o, it prints Found o.
- Otherwise, it prints the character itself.
Output:
H e l l Found 'o' W Found 'o' r l d
Here’s another simple example of counting vowels in a string:
word = "Programming" vowels = "aeiouAEIOU" count = 0 for letter in word: if letter in vowels: count += 1 print("Number of vowels:", count)
Output:
Number of vowels: 3
This code loops through each letter of the word Programming, checks if it's a vowel, and increments the count variable accordingly.
Looping through a string is useful when you need to inspect or manipulate individual characters.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What will be the output of the following code? word = "Python" for letter in word: print(letter)
CP y t h o n
DP
AP
y
t
h
o
n
BPython
Check Answer
What will be the output of the following code? word = "Python" i = 0 while i < len(word): print(word[i]) i += 1
AP
y
t
h
o
n
BPython
CP y t h o n
DP
Check Answer
What is the output of the following code? word = "Python" for i in range(len(word)): print(word[i])
CP y t h o n
DP
AP
y
t
h
o
n
BPython
Check Answer
What will be the output of the following code? sentence = "Hello World" for char in sentence: if char == 'o': print("Found 'o'") else: print(char)
AH
e
l
l
Found 'o'
W
Found 'o'
r
l
d
BHello World
CH e l l o W o r l d
DFound 'o'
Check Answer
What will be the output of the following code? word = "Programming" vowels = "aeiouAEIOU" count = 0 for letter in word: if letter in vowels: count += 1 print("Number of vowels:", count)
CNumber of vowels: 4
DNumber of vowels: 1
ANumber of vowels: 2
BNumber of vowels: 3
Check Answer