Output of Python programs | Set 7

30 Sep, 2019
Prerequisite – Strings in PythonPredict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language.
Program 1var1 = 'Hello Geeks!'    var2 = "GeeksforGeeks"  print "var1[0]: ", var1[0]        # statement 1print "var2[1:5]: ", var2[1:5]    # statement 2Output:var1[0]:  H
var2[1:5]:  eeks
Explanation:Strings are among the most popular types in Python. We can create a string by enclosing characters within quotes. Python treats single quotes same as double quotes. It is notable that unlike C or C++ python does not support a character type; in fact single characters are treated as strings of length one, thus also considered a substring. To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.Statement 1: will simply put character at 0 index on the output screen.Statement 2: will put the character starting from 0 index to index 4.
var1 = 'Hello Geeks!'    var2 = "GeeksforGeeks"  print "var1[0]: ", var1[0]        # statement 1print "var2[1:5]: ", var2[1:5]    # statement 2
Output:
var1[0]:  H
var2[1:5]:  eeks

Explanation:Strings are among the most popular types in Python. We can create a string by enclosing characters within quotes. Python treats single quotes same as double quotes. It is notable that unlike C or C++ python does not support a character type; in fact single characters are treated as strings of length one, thus also considered a substring. To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.Statement 1: will simply put character at 0 index on the output screen.Statement 2: will put the character starting from 0 index to index 4.
Program 2var1 = 'Geeks'  print "Original String :-", var1  print "Updated String :- ", var1[:5] + 'for' + 'Geeks' # statement 1Output:Original String :- Geeks
Updated String :-  GeeksforGeeks
Explanation:Python provides a flexible way to update string in your code. Use square brackets and specify the index from where string has to be updated and use + operator to append the string. [x:y] operator is called Range Slice and gives the characters from the given range.Statement 1: In the given code tells the interpreter that from 5th index of string present in var1 append ‘for’ and ‘Geeks’ to it.
var1 = 'Geeks'  print "Original String :-", var1  print "Updated String :- ", var1[:5] + 'for' + 'Geeks' # statement 1
Output:
Original String :- Geeks
Updated String :-  GeeksforGeeks

Explanation:Python provides a flexible way to update string in your code. Use square brackets and specify the index from where string has to be updated and use + operator to append the string. [x:y] operator is called Range Slice and gives the characters from the given range.
Statement 1: In the given code tells the interpreter that from 5th index of string present in var1 append ‘for’ and ‘Geeks’ to it.
Program 3para_str = """this is a long string that is made up ofseveral lines and non-printable characters such asTAB ( \t ) and they will show up that way when displayed.NEWLINEs within the string, whether explicitly given likethis within the brackets [ \n ], or just a NEWLINE withinthe variable assignment will also show up."""print para_strOutput:this is a long string that is made up of
several lines and non-printable characters such as
TAB (      ) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given like
this within the brackets [ 
 ], or just a NEWLINE within
the variable assignment will also show up.
Explanation:Python’s triple quotes comes to the rescue by allowing strings to span multiple lines, including NEWLINEs, TABs, and any other special characters.The syntax for triple quotes consists of three consecutive single or double quotes.
para_str = """this is a long string that is made up ofseveral lines and non-printable characters such asTAB ( \t ) and they will show up that way when displayed.NEWLINEs within the string, whether explicitly given likethis within the brackets [ \n ], or just a NEWLINE withinthe variable assignment will also show up."""print para_str
Output:
this is a long string that is made up of
several lines and non-printable characters such as
TAB (      ) and they will show up that way when displayed.
NEWLINEs within the string, whether explicitly given like
this within the brackets [ 
 ], or just a NEWLINE within
the variable assignment will also show up.

Explanation:Python’s triple quotes comes to the rescue by allowing strings to span multiple lines, including NEWLINEs, TABs, and any other special characters.The syntax for triple quotes consists of three consecutive single or double quotes.
Program 4print 'C:\\inside C directory' # statement1  print r'C:\\inside C directory' # statement2Output:C:\inside C directory
C:\\inside C directory
Explanation:Raw strings do not treat the backslash as special characters at all.Statement 1 : will print the message while considering backslash as a special character.Statement 2 : is a raw string that will treat backslash as a normal character.
print 'C:\\inside C directory' # statement1  print r'C:\\inside C directory' # statement2
Output:
C:\inside C directory
C:\\inside C directory

Explanation:Raw strings do not treat the backslash as special characters at all.Statement 1 : will print the message while considering backslash as a special character.Statement 2 : is a raw string that will treat backslash as a normal character.
Program 5print '\x25\x26'Output:%&
Explanation:In the above code \x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character. Hence the corresponding symbols will be on the output screen.
print '\x25\x26'
Output:
%&

Explanation:In the above code \x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character. Hence the corresponding symbols will be on the output screen.
This article is contributed by Avinash Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
Python-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Runtime Errors
Different ways to copy a string in C/C++
Output of C Programs | Set 3
Output of C++ Program | Set 1
Output of Java Program | Set 3
Output of C++ programs | Set 47 (Pointers)
Output of Java Programs | Set 12
Output of Java Program | Set 7
Output of C programs | Set 59 (Loops and Control Statements)
unsigned specifier (%u) in C with Examples