Output of Python programs | Set 7 Last Updated : 06 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Prerequisite - Strings in Python Predict the output of the following Python programs. These question set will make you conversant with String Concepts in Python programming language. Program 1 Python var1 = 'Hello Geeks!' var2 = "GeeksforGeeks" print "var1[0]: ", var1[0] # statement 1 print "var2[1:5]: ", var2[1:5] # statement 2 Output:var1[0]: Hvar2[1:5]: eeksExplanation: 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 2 Python var1 = 'Geeks' print "Original String :-", var1 print "Updated String :- ", var1[:5] + 'for' + 'Geeks' # statement 1 Output:Original String :- GeeksUpdated String :- GeeksforGeeksExplanation: 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 3 Python para_str = """this is a long string that is made up of several lines and non-printable characters such as TAB ( \t ) and they will show up that way when displayed. NEWLINEs within the string, whether explicitly given like this within the brackets [ \n ], or just a NEWLINE within the variable assignment will also show up. """ print para_str Output:this is a long string that is made up ofseveral lines and non-printable characters such asTAB ( ) and they will show up that way when displayed.NEWLINEs within the string, whether explicitly given likethis within the brackets [ ], or just a NEWLINE withinthe 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 4 Python print 'C:\\inside C directory' # statement1 print r'C:\\inside C directory' # statement2 Output:C:\inside C directoryC:\\inside C directoryExplanation: 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 5 Python 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. Comment More info A Avinash Kumar Singh Improve Article Tags : Python Python Programs Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 5 min read Recursion in Python 6 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 5 min read Python Tuples 4 min read Dictionaries in Python 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like