Write a python program that traverses through an input string and prints its characters indifferent lines – two characters per line
Question
Write a python program that traverses through an input string and prints its characters indifferent lines – two characters per line
Solution
Sure, here is a simple Python program that does that:
def print_chars(input_string):
for i in range(0, len(input_string), 2):
print(input_string[i:i+2])
# Test the function
print_chars('Hello, World!')
This program defines a function print_chars
that takes an input string. It then loops over the string, taking two characters at a time (this is what input_string[i:i+2]
does), and prints them. The range(0, len(input_string), 2)
part means "start at 0, go up to the length of the string, and increase by 2 each time", which is how we get two characters at a time.
Similar Questions
Write a python program to print the number of occurrences of a substring into a line
Write a python program to: Read two strings, then put the second string in the middle ofthe first string. And finally Write the new string in a new file.
What will the output of the following program be?L1 = ['Vikas', 'Akshay', 'Sanskar', 'Mahima']print(L1[1][-1])
#Write a program to create a text file and print the lines starting with ‘T’ or ‘P’. (Both# uppercase and lowercase)
2. What will be the output of the following Python code?>>>str="hello">>>str[:2]>>>
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.