In this instructional exercise, you will figure out how to iterate strings in Python. You could utilize a for loop, range in Python, a slicing administrator, and a couple more strategies to navigate the characters in a string.
Iterate Strings In Python
Coming up next are different approaches to iterate the burns in a Python string. How about we initially start with the for-loop strategy?
Using for loop to traverse a string
It is the most conspicuous and clear strategy to iterate strings. Follow the beneath test code:
""" Using for loop to iterate over a string in Python """ strin_iterate = "Data Science" for char in string_iterate: print(char)
Output:
D a t a S c i e n c e
Range to iterate over a string in Python
Another basic method to cross the string is by utilizing the Python range work. This technique allows us to get to string components utilizing the list.
Model code is given down:
""" Python Program Example: Using range()to iterate over a string in Python """ string_iterate= "Data Science" for char_index in range(len(string_iterate)): print(string_iterate[char_index])
Output:
D a t a S c i e n c e
Python Slice operator to iterate strings partially
You can navigate a string as a substring by utilizing the Python slice operator ([]). It’s anything but a substring from the first string and subsequently permits it to iterate over it part of the way.
Syntax:
# Slicing Operator string [starting index : ending index : step value]
To utilize this technique, give the beginning and finishing lists alongside a stage worth and afterward cross the string. The following is the model code that iterates over the initial six letters of a string.
""" Using slice[] operator to iterate over a string partially """ string_to_iterate= "Python Data Science" for char in string_to_iterate[0 : 6 : 1]: print(char)
Output:
P y t h o n
You can utilize the slice operator use further by utilizing it to iterate over a string while leaving each substitute character. Look at the underneath model:
""" Using slice[] operator to iterate over a specific parts of a string """ string_to_iterate= "Python_Data_Science" for char in string_to_iterate[ : : 2]: print(char)
Output:
P t o _ a a S i n e
Traverse string backward using the slice operator
On the off chance that you pass a -ve step worth and skirt the beginning and finishing records, you can iterate the retrogressive way. Go through the given code test.
""" Using slice[] operator to iterate strings backward """ string_iterate= "Machine Learning" for char in string_iterate[ : : -1]: print(char)
Output:
g n i n r a e L e n i h c a M
Using indexing to iterate strings backward
The slice operator initially produces a switched string, and afterward, we utilize the for loop to cross it. Rather than doing it, we can utilize ordering to iterate strings in reverse.
""" Using indexing to iterate strings backward """ string_iterate="Machine Learning" char_index= len(string_iterate) - 1 while char_index >= 0: print(string_iterate[char_index]) char_index -= 1
Output:
g n i n r a e L e n i h c a M
On the other hand, we can pass the – ve list worth and cross the string in reverse. See the beneath model.
""" Using negatve index to iterate strings backward """ string_iterate= "Learn Python" char_index= 1 while char_index<= len(string_iterate): print(string_iterate[-char_index]) char_index += 1
Output:
n o h t y P n r a e L
Summary of all
We should now combine all models inside the Main() work and execute from that point.
""" Python Program:
Python Program to iterate strings char by char """ def Main(): string_iterate="Data Science" for char in string_iterate: print(char) string_iterate="Data Science" for char_index in range(len(string_iterate)): print(string_iterate[char_index]) string_iterate="Python Data Science" for char in string_iterate[0 : 6 : 1]: print(char) string_iterate="Python_Data_Science" for char in string_iterate[ : : 2]: print(char) string_iterate="Machine Learning" for char in string_iterate[ : : -1]: print(char) string_iterate="Machine Learning" char_index=len(string_iterate) - 1 while char_index>= 0: print(string_iterate[char_index]) char_index -=1 string_iterate="Learn Python" char_index= 1 while char_index<= len(string_iterate): print(string_iterate[-char_index]) char_index+= 1 if __name__== "__main__": Main()
Output:
D a t a S c i e n c e D a t a S c i e n c e P y t h o n P t o _ a a S i n e g n i n r a e L e n i h c a M g n i n r a e L e n i h c a M n o h t y P n r a e L
Recommended Posts:
- MySQL Create User with Password Explained with Examples – MPS
- SQL Exercises with Sample Tables and Demo Data – MPS
- MySQL LOWERcase/LCASE() Functions with Simple Examples – MPS
- MySQL Date and Date Functions Explained with Examples – MPS
- MySQL CURRENT_TIMESTAMP() Function Explained with Examples – MPS
- Grant Privileges on a Database in MySQL with Examples – MPs
- MySQL vs PostgreSQL Comparison – Know The Key Differences – MPS
- MySQL UPSERT | Three Techniques to Perform an UPSERT – MPs
- MySQL FROM_UNIXTIME() Function Explained with Examples – MPS