Page 226 - Robotics and AI class 10
P. 226
Traversing a String
Traversing means visiting each element of a string and processing it as required by a program. We use a loop to
traverse a string.
There are different ways of visiting each element as shown below:
Txt="WONDERFUL" W*O*N*D*E*R*F*U*L*
for indx in Txt:
print(indx,end="*")
Txt="WONDERFUL" W!O!N!D!E!R!F!U!L!
for i in range(len(Txt)):
print(Txt[i],end="!")
Task
#Coding & Computational Thinking
Find out the output of the given code:
Txt="NATURE"
for i in range(len(Txt)):
print(i, end="@")
Multiline Strings
Multiline strings in Python allow you to define strings that stretch over multiple lines. There are a few ways to
create multiline strings:
• Using triple quotes (" " "):
multiline_str = " " "
This is my favorite book.
It is very interesting to read.
" " "
print(multiline_str)
Output:
This is my favorite book.
It is very interesting to read
• Using the escape character \ to continue the string on the next line:
multiline_str = "
This is my favorite book. \
It continues on the next line.\
It is very interesting to read.”
print(multiline_str)
Both approaches will produce the following output:
224 Touchpad Robotics & Artificial Intelligence-X

