Introduction to strings in python:
String is a sequence of characters which includes letters, numbers and symbols like punctuation marks etc. In simple words string is the data in text format. Strings data type also contains white spaces in it.
Text data is one of the mostly used data type 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 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 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 total 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 opening 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 string out of multi lines. If you want to define complete paragraph as a string in that case you can use triple single quote method of string creation.
You can use any one of the above mentioned way 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 above sentence in single quotes then your program will show a syntax error while execution.
The reason behind the syntax error is that, Python interpreter will consider the first single quote as a string’s starting point and successive single quote present in word can’t as a string’s end point. 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 the 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 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:
Concatenation of string means joining two different strings to create new separate string.
To join two different strings + sign is used. While working with the numeric data type + sign is considered as addition operator but in case of strings data type + sign is considered as joining operator.
In example below you can see that, we are having two different strings i.e first one is “My Name is:” and 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 certain operation on string repetitively. To do this work there is 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.
Lets 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 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 variable as an empty box in which you can store the values. Storing the strings in variable basically makes it easy to work with the strings.
There is separate article written for the variable and data types in python you can refer that as well to more information about variables.
Now lets see one example of how to store the strings in variable and then we will try to concatenate them. I want to print sentence “Jim and John are best friends.” But in this sentence I want to store the names “Jim and John” as a variable.
In 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 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 are 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 help of the escape sequence.
Escape sequence gets started with backslash (\). When you include backslash in your string, 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 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 string and should get printed on the screen. So we used the escape character in front of double quotes and interpreter considered them as an integral part of the string.
List of escape characters:
Like above example there are many ways we can use 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