Page 227 - Robotics and AI class 10
P. 227
Traversing a String Output:
Traversing means visiting each element of a string and processing it as required by a program. We use a loop to This is my favorite book.
traverse a string. It is very interesting to read
There are different ways of visiting each element as shown below:
Brainy Fact
Txt="WONDERFUL" W*O*N*D*E*R*F*U*L*
for indx in Txt:
ASCII Stands for "American Standard Code for Information Interchange." It is a character encoding
print(indx,end="*") standard that uses numeric codes to represent characters in upper and lower case, numbers, and
punctuation symbols.
Txt="WONDERFUL" W!O!N!D!E!R!F!U!L!
ASCII value of capital letters A–Z is 65 to 90, small letters is 97 to 122 and numbers 0 to 9 is 48 to 57.
for i in range(len(Txt)):
print(Txt[i],end="!")
Concatenating Strings
Task Concatenating strings in Python involves combining multiple strings into a single string.
#Coding & Computational Thinking There are a few ways to achieve this:
• Using the Addition operator (‘+’) is known as the concatenation operator with the string values as it works
Find out the output of the given code: to join the string values.
Txt="NATURE" For example:
for i in range(len(Txt)): str1 = "Hello"
print(i, end="@") str2 = "World!"
result = str1 + " " + str2
Multiline Strings print(result)
Output:
Multiline strings in Python allow you to define strings that stretch over multiple lines. There are a few ways to
create multiline strings: Hello World!
• Using triple quotes (" " "): • Using the Augmented Assignment Operator(“+=”): In Python, the += operator is used for concatenating
multiline_str = " " " strings and assigning the concatenated result back to the original string variable. It provides a concise way to
append additional text or characters to an existing string.
This is my favorite book.
It is very interesting to read. For example:
str1 = "Hello"
" " "
str2 = "World"
print(multiline_str)
str1 += " " + str2
Output:
print(str1)
This is my favorite book.
Output:
It is very interesting to read Hello World
• Using the escape character \ to continue the string on the next line:
• Using the Join() method: The join() method in Python is a string method that concatenates a sequence of
multiline_str = "
strings into a single string using a specified delimiter.
This is my favorite book. \ For example:
It continues on the next line.\
str_list = ["Hello", "World"]
It is very interesting to read.”
concatenated_str = " ".join(str_list)
print(multiline_str)
print(concatenated_str)
Both approaches will produce the following output:
Output:
Hello World
Introduction to Data and Programming with Python 225

