Page 108 - Computer - 8
P. 108
Strings can be assigned to a variable in two ways:
1. Enclosing the value in double quotes.
2. Accepting values from the user using the input( ) function.
For example,
Name = “DataMine”
Name = input(“Enter the name of the student : ”)
Knowledge Discovery Subject Enrichment
The input( ) function converts any input value typed by the user into a string. Thus, the number 1234
entered by the user will be converted to the string value “1234”. A string that has no characters between
the double quotes, such as “ ”, is considered an Empty string.
Multiline Strings
Multiline strings can be created by enclosing the characters within triple quotes (“““ ”””). Any quotes,
tab spaces, or special characters are considered a part of the string.
Lab Activity 4
To create a multiline string and display it. Experiential Learning
1. Type the code as shown in the image in the Python Editor.
2. Press the F5 key to execute the code. Sample output of the program is given below.
Knowledge Discovery Subject Enrichment
An Escape sequence is a sequence of characters that, when used inside a character or string, do not
represent itself but are converted into another character or series of characters. Escape sequences are
used to start a new line, insert tab spaces between text, or display special characters, such as double
quotes or single quotes. An escape sequence starts with a backslash ( \ ), followed by the symbol that
represents the sequence to produce. For example, ‘\n’ is an escape sequence used to insert a new line,
while \\’ is an escape sequence that displays double quotes.
Traversing a String
Traversing a string is the process used to access the characters in a string as required. Each character
in a string is assigned a position called its Index. The leftmost character is assigned the index 0 and the
106