How to Use String in Python Programming Language

Posted on


This article will focus to learn string in python programming language. String itself actually is a character data type. In this context, the demonstration of using string in python programming language will be available in Windows operating system using python interpreter. The execution of the python interpreter is done in the command line interface. That command line interface is the Command Prompt tool. So, this is how to print string in command line interface :

Microsoft Windows [Version 10.0.18362.1016]
(c) 2019 Microsoft Corporation. All rights reserved.

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Using single quote to enclose string data

The first one is to define a variable with string data. Just enclose it with single quote before and after the string data. The following is an example for defining and storing it into a variable. Next, right after defining it, print the variable in the command line interface as follows :

>>> text1 = 'this is a string'
>>> print(text1)
this is a string

As in the above output, a built-in function with the name of ‘print’ is available to print the content of the variable with the name of text1. So, in the next line after, the output of the content is printed.


Printing a single quote string

Continue on the previous part, how about printing the actual single quote ?. Basically, enclosing a string containing a single quote can be a troublesome problem as follows :

>>> text2 = 'this isn't a numeric'
File "<stdin>", line 1
text2 = 'this isn't a numeric'


SyntaxError: invalid syntax


As in the above output, there is an error exist. Not even the error exist in the execution of the print command but the error actually appear in the middle of the variable definition. So, what is the solution to solve the above problem ?. There are two solutions for solving the above problems. The first solution is using an escape character before the single quote character so that it will be considered as a text character. What is the escape character actually looks like ?. It is actually the backslash character (‘\’} representing the escape character. The latter will use double quotes characters to enclose the string.


Using an escape character to print single quote string

So, in order to print a single quote character available in a string variable, just add an escape character before it. This character will force the single quote character to be a normal character instead of a closing character for defining data string in a variable. Just take a look in the following example :

>>> text2 = 'this isn\'t a numeric'
>>> print(text2)

Using double quotes to print single quote string

Another solution is to use double quotes to print single quote characters. So, the double quotes is actually the one acting as an opening and closing character for defining the start and the end of data string definition into a variable instead of the single quote characters. The following is the actual example for the double quotes usage :

this isn’t a numeric

>>> text3 = "this isn't a numeric"
>>> print(text3)

Leave a Reply