Introduction to strings in python:
A string is a sequence of characters that include letters, numbers, and symbols like punctuation marks, etc. In simple words, a string is the data in text format. Strings data type also contains white spaces in it.
Text data is one of the most used data types in our day to day life. Let’s see one example of strings, In python “Apple” is a string and sentence “I ate an apple” is also a string, not only that but “123789” is also a string provided it is specified in a valid format.
That was the definition of python string data type. Now in this article, we are going to cover, how to create strings in python. Also, the operation that can be performed on strings like string concatenation and string replication in python.
How to create strings in python:
In python to create strings in you have to include them within the quotation marks either single or double. Whatever included within the quotation marks will be treated as a string. There are three ways of string creation in python.
- String included in opening and closing single quote. e.g ‘ This is string with single quotes. ‘
- Included in opening and closing Double quotes. e.g ” It is a string in double quotes. “
- Include in three single openings and three closing single quotes. e.g ”’ This is string in three single quotes. ”’. This method of string creation is mostly used where you want to create a string out of multi-lines. If you want to define a complete paragraph as a string in that case you can use a triple single quote method of string creation.
You can use any one of the above-mentioned ways of string creation in your python program. But there are some cases wherein you have to go with the specific string creation format.
for example, I want to create a string of below sentence.
'I can't wait to see you.'
Now in your python code, if you write the above sentence in single quotes then your program will show a syntax error while execution.
The reason behind the syntax error is that the Python interpreter will consider the first single quote as a string’s starting point and successive single quote present in the word can’t as a string’s endpoint. From the complete sentence “I can’t wait to see you.” it will only consider I can part as a string. For the python interpreter remaining portion of the sentence is not a string, since there is no opening single quote present for it. It will get confused about, what to do with the remaining portion of the sentence, so it will show an error.
To solve the above problem we have to use other ways of string creation. That is you can either use double quotes or triple single quotes to handle such specific cases. Let’s see one more case. How to create a string for the following sentence.
She said: "I don't know"
In this sentence both the double as well as single quotes are important and you want to keep both of them. In this case, we will prefer the third way of string writing i.e Include the complete string in triple single quotations.
Now that was all about the strings creation in python.
What is string concatenation in python:
The concatenation of string means joining two different strings to create a new separate string.
To join two different strings + sign is used. While working with the numeric data type + sign is considered as an addition operator but in case of strings data type + sign is considered as joining operator.
In the example below you can see that, we are having two different strings i.e first one is “My Name is:” and the second one is ” John”. We have concatenated both the string using + operator.
What is string replication in python:
Sometimes we may be required to perform a certain operations on string repetitively. To do this work there is a string replication operator * available. Asterisk sign * when used with numeric data type it performs multiplication operation, But when the * sign (string replication operator) used with one string and one number it performs string replication operation.
Let us see one example, Suppose I want to print string “John” five times. To do this work I’ll use string replication. In your program write below the line of code:
print("John " * 5)
How to store the strings in variables and print them in python:
Variables in python is a space reserved in which you can store your strings data. You can consider a variable as an empty box in which you can store the values. Storing the strings in the variable basically makes it easy to work with the strings.
There is a separate article written for the variable and data types in python you can refer that as well to more information about variables.
Now let us see one example of how to store the strings in variable and then we will try to concatenate them. I want to print the sentence “Jim and John are best friends.” But in this sentence, I want to store the names “Jim and John” as a variable.
In the above example, you can see, We have created two variables “variable1 and variable2”. We have assigned the strings “John and Jim” to each of them. Then in the print statement we have concatenated them using concatenation operator. Also, we have inserted two strings “and “, “are the best friends. ” directly is the print statement which is not stored in any variable.
What are the escape characters in python:
For some of the characters in our string, if we want the interpreter should give the special treatment then we have to take the help of the escape sequence.
Escape sequence gets started with a backslash (\). When you include a backslash in your string, the python interpreter understands that we have to treat the next character differently.
Suppose there is a string “I read the article, ‘A Poor Woman’s Journey.'”. This sentence contains both the single as well as double quotes in it. Now I want to print this string as it is with all the quotes printed.
As you can see, We have used the backslash (\) in front of each double quote. Due to that interpreter treated those double-quotes separately. We wanted the double quotes to be considered as part of the string and should get printed on the screen. So we used the escape character in front of double quotes and the interpreter considered them as an integral part of the string.
List of escape characters:
As the above example there are many ways we can use an escape sequence. Here is the list of complete escape characters along with its usage.
You can also get more help of python form the python forum: Click Here