In this article, Understanding Python Strings in the Python language. Characters are the basic block to make a string and there is no language in the world that does not contain characters.
1. What is Python string
“A string in Python is a derived data type that is represented as a sequence of characters.” strings are one of the most demanding data types used by all types of python developers. Strings are Arrays of bytes of Unicode characters. Since the string is an array so we can use Square brackets to access elements of the string.
We can easily parse a string in python using indexes. Like most programming languages indexing starts from 0 in python also and it ends with n-1 for a character string. So the first character can be accessed at index 0 and the last character can be accessed at n-1 index.
2. How python strings work
There are two ways To declare a Strings in python.
- Single quotation or double quotation marks.
- Python Mutiline string.
2.1 Single quotation or double quotation marks
In python, you can use single quotes or double quotes to declare a string. Both are treated the same. We can assign the string to a variable by just using the equal sign as:
"Double quotes are used to define a string variable value." 'Single quotes are used to define a string variable value.'
Program exmaple Using Single Quotes or Double Quotes:
#Double Quotes: var Greeting = "Hello" var Greeting2 = "Bonjour" name = "Sachin" #singel Quotes var Greeting= 'Welcome to Japan!' name = 'Sachin'
As you might know, the character data type is used in many languages, but Python does not have it. So to single-character string is used to store character variables.
var char = "B"
2.2 Python Multi line strings
Now if you have a long string that goes beyond one line then python provides you multi-line string support also.we enclosed string in pair of triple-double quotes ” ” ” or single quotes(‘ ‘ ‘) at beginning and end and assign it to a variable.
Syntax for Multi line strings are :
var Greeting= '''Welcome to Japan!''' var Greeting2= """Welcome to Australia"""
3.Access elements of string
And to access the first character we will write PlayerName[-n] where n is the length of your string. You can use ‘len(…)’ function to find the length of your string.
So if we try to access Playername, we will get ‘G’, PlayerName, we will get ‘o’. We can access a string character from the end by using negative indexing.
Program code Access elements of string
PlayerName = "Good Morning! This is Sachin Tendulkar" print(PlayerName[-1]) print(PlayerName [-2]) print(PlayerName[-len(PlayerName)])
r a G
5.Python strings are immutable
The string in python are immutable and the content of a string cannot be changed. So any attempt to modify it will result in an error. If we try the above line of code this will result in an error.
PlayerName = "Good Morning! This is Sachin Tendulkar" PlayerName = 'H'
TypeError: 'str' object does not support item assignment
6. Out of range error in a python string
Once we declare a string in Python then it has a fixed size and length. This access is limited within a range and that range the first index which is 0 and the last index which is length – 1.
If we try to access an index that is greater than 5, means bigger than the length -1. Then we will get an Out of range error. name # Booom!!!!
If you will try to access a character index that is greater than the length of the string length, then python will prompt you with an error. This is called out of range error.
name = "Sachin" print(name) # 's' first character print(name) # 'n'. Here we are accessing last character.
7. How to use quotes in Python strings?
The string declaration below that we use single and double-quotes. What if we want to display quotes, for example.
This is interesting to know how we can do this , because we have multiple double quotes in this string and this can confuse us.
"Sachin said,"India will win World cup next Year.""
Two ways to do this in Python
- Using \ escape character
- Mixing single and double quotes
7.1 Using \ escape character
We use the \ escape character to ignore additional quotes. If you see the below line of code, we are using \(escape character) before “India“. the \ will suppress “(double-quotes)which is just after \ and will treat this as text-only. Similarly, at the end of this string, we are using \ to suppress the ” again.
print("Sachin said, \"India will win World cup next Year.\"")
Sachin said, "India will win World cup next Year."
7.2.Mixing single and double quotes
Another way is by mixing double and single quotes. So if we have a string that contains the double quotes statement then we can use the single quotes for the whole string and double quotes for the statement with double quotes.The below example will help you understand this better.
print('Sachin said,"India will win World cup next Year."')
We hope you found this string introduction interesting with a lot of information about strings in Python. We will explain more details in specific posts about some of the features we talked about in this post.
So keep Learning with us!!