Python Programming for Beginners Lesson 3: Introduction to Strings
Introduction to Strings in Python
A Brief Overview of Python
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python’s design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. Python is often described as a “batteries included” language due to its comprehensive standard library.
Python was conceived in the late 1980s as a successor to the ABC language. Python 2.0, released in 2000, introduced features like list comprehensions and a garbage collection system with reference counting.
Python 3.0, released in 2008, was a major revision of the language that is not completely backward-compatible, and much Python 2 code does not run unmodified on Python 3.
The Python 2 language was officially discontinued in 2020 (first planned for 2015), and “Python 2.7.18 is the last Python 2.7 release and therefore the last Python 2 release.” No more security patches or other improvements will be released for it. With Python 2’s end-of-life, only Python 3.5.xand later are supported.
Python interpreters are available for many operating systems. A global community of programmers develops and maintains CPython, a free and open-source reference implementation. A non-profit organization, the Python Software Foundation, manages and directs resources for Python and CPython development.
What exactly are Strings in Python?
In the simplest of terms, Strings in Python represent the text or information that we use to convey our feelings/opinions etc. in our everyday life. That is what makes Strings such an important data structure in Python. With the use of Strings we can display error messages, let the user know when he needs to enter an input to the program, describe what our code does and so much more. A String in Python is generally represented by text inside two single quotes (‘’) or two double quotes (“”). An example of a String in Python would be the word “hello”. Notice how it is inside the double quotes, this lets the Python compiler know that “hello” in our program is meant to be a String. To store the value of a String in some container like we did in the variables chapter, we can use a variable. And remember how I told you that Python is a dynamically typed language? That means we can use any variable name to store our String data, it need not be anything specific. For example I can do name = “Ziyad” and this will store the string called “Ziyad” in the variable called name. The variable name can be anything, it can be called ‘a’, ‘b’ or anything else that you want to name it.
Now let’s check the datatype of our name variable. We can achieve this using print(type(name)). This will return the class ‘str’ denoting that it is indeed a string. Now if we have to use a double quote inside our string, then we shall have to enclose our string in single quotes. Alternatively, if we have to use single quotes on our string, then we shall have to enclose it in double quotes. An example would be: “Hi there! I hope you don’t mind, but can you help me out with this?” Notice how don’t has a single quote in it. If we would have used single quotes to enclose the entire string, then at the point where the single quote of don’t appears, Python would assume that it is the end of the string. So in order to avoid such a situation we used double quotes. Alternatively if there are double quotes inside the string, we shall use single quotes. Another case might be when we have a very long piece of text like in the image shown below, then we should use triple single quotes or triple quotes to enclose it. Do keep in mind that this will preserve the line spacing exactly as it is in the output as well.
To store the results, we can once again make use of variables and then use the print statement of Python to output the results to the console. An example would be message = ‘John said to me “I will see you later” ‘. A very long string enclosed in triple single quotes can be seen in the figure below along with the output.
Input to our Python program:
Corresponding output of our Python program:
Notice how our Python program outputs the type of the variable called ‘name’ and also outputs the string content of the variable called ‘romeo’.
Writing our own ‘Hello World’ Program
We are now equipped with more than enough knowledge to tackle the world famous ‘Hello World’ program. To achieve this all we need to do is take any variable, let’s say our variable in this case is called ‘hello’ and then assign the string ‘Hello World’ to the variable ‘hello’. After this all we need to do is print the contents of the variable ‘hello’ to our console. This can be achieved as follows: hello = ‘Hello world!’ followed by print(hello). The figures below will make the concept even clearer.
Input to our Python program:
Corresponding output of our Python program:
Taking in Strings as user inputs
To take in Strings as inputs from the user instead of hardcoding them into the variable in the program itself, we can use the input() function of Python. However do keep in mind that the input() function always converts the variable type to a String. For example if I do something like var = input(“Please enter a number: “) and once the user types in a number in the console and I do print(type(var)) then it will return class <str> implying that even though I asked the user to type in a number, Python will return the datatype of the entered number as a String, because the input() function always returns a String. The code snippet below will make things clearer. One thing to note here is the sentences after the # symbol. These are called comments. Comments are ignored by the Python compiler and are usually just to describe what our code does.
Code of our Python program:
In this code we are asking the user to enter his/her name first. Then we are asking them to input their age. Now name will be a String as expected. However age will also get converted to a String by Python, instead of an integer because input() function can only take in strings. The output figure of our code snippet attached below will make things clear. Also notice the red lines starting with a # symbol. Those are the comments I was talking about. They are there only for the purpose of describing what the code does.
Output of our Python program:
We can enhance our code even further by taking in even more inputs from our users like, Which city they live in? What do they enjoy doing the most? Etc. This can be done using something like city = input(“Where do you live?: ”) or love = input(“What do you like doing the most?: ”). Do not get confused by the colons at the end of the question, they are just there to give the user a little bit of space between the questions and inputs. Following code snippet will make things clearer.
So with this, we come to the end of taking in Strings as user inputs and how we can use them in our Python code to make them much more elegant. Always remember, appropriate use of Strings in your Python code can increase user interactivity by many folds since it makes your code much more readable and convenient for new users. If you ever face any confusion regarding the basics of Strings or get stuck in some code, then you can always come back and refer this guide.