Python Programming Language Tutorial #2 [2020] Strings in Python -DDSRY
In this #2 post of Python Tutorial, I will explain to you about String in Python Programming Language. in Previous posts, I have explained to you, What is Python Programming? and Hello World Program in Python.
Strings in Python Programming:
Text values are known as a string in Python Programming Language.
Anything was written between double quotes “ “ or single quote ‘ ‘ is
a string in python.
When programming text is represented by strings in Python there are many ways to create a string, after all, there are many different types of text some have apostrophes some have quotation marks and some long string shave new lines your goal today make strings in three different ways.
To begin open Python IDLE, first, we create a string and store it in a variable.
This stores the learn about string in python in a variable called strings_variable, you do not have to type a semicolon at the end as you do in many other languages like Java or C++.
You can see the strings stored in strings_variable by printing using print() built-in function in Python. We created our first-string using quotation marks but you can also create strings using single quotes.
string_single_quotes=’ learn to store a string in a variable — Python’
string_single_quotes is a variable name and ‘learn to store the string in the variable is stored in string_single_quotes.
let’s verify that the text is stored in this variable by printing it (using print()) built-in function, so you can make a string using single quotes or double quotes we didn’t have to specify that message and message two were strings we just assign them strings and the Python interpreter was smart enough to know that the variables should hold strings
but what’s the point of this to have more than one way to make a string well suppose you wanted to make a string and one of the words had an apostrophe if you try to make the string using single quotes you get a syntax error, see below image.
do you see the problem when the Python interpreter encountered the apostrophe in the word I’m it thought that that was the end of the string so the remaining text caused the error, there are two ways to fix this.
One way is to escape the apostrophe by putting a backslash in front of it this tells python that the single quote is to be treated as a single character and not the end of the string.
but the second way is to create the string using double quotes, no errors no escape characters if you make a string using double quotes but your text contains a quotation mark you get another error.
this is because Python interprets raise error the quotation mark, as the end of the string we can avoid this error by using single quotes to make the string.
But how do you make more complicated strings, string which contain apostrophes and quotation marks for this case you can,
Thanks for Reading.🙏
DDSRY
Originally published at https://ddsry.com on April 9, 2020.