Python String
Python string is the collection of the characters surrounded by single quotes, double
quotes, or triple quotes.
Each character is encoded in the ASCII or Unicode character. So we can say that
Python strings are also called the collection of Unicode characters.
Consider the following example in Python to create a string.
Syntax:
1. str = "Hi Python !"
Here, if we check the type of the variable str using a Python script
1. print(type(str)), then it will print a string (str).
Creating String in Python
We can create a string by enclosing the characters in single-quotes or
double- quotes. Python also provides triple-quotes to represent the string,
but it is generally used for multiline string or docstrings.
1. #Using single quotes
2. str1 = 'Hello Python'
3. print(str1)
4. #Using double quotes
5. str2 = "Hello Python"
6. print(str2)
7.
8. #Using triple quotes
9. str3 = '''''Triple quotes are generally used for
10. represent the multiline or
11. docstring'''
[Link](str3)
Output:
Hello Python
Hello Python
Triple quotes are generally used for
represent the multiline or
docstring
>>> str3 = '''''Triple quotes are generally used for
represent the multiline or
docstring'''
>>> print(str3)
''Triple quotes are generally used for
represent the multiline or
docstring
--------------------------------------------------------
>>> str3 = '''''Triple quotes are generally used for
represent the multiline or docstring'''
>>> print(str3)
''Triple quotes are generally used for
represent the multiline or docstring
Strings indexing and splitting
Like other languages, the indexing of the Python strings starts from 0. For
example, The string "HELLO" is indexed as given in the below figure.
Consider the following example:
1. str = "HELLO"
2. print(str[0])
3. print(str[1])
4. print(str[2])
5. print(str[3])
6. print(str[4])
7. # It returns the IndexError because 6th index doesn't exist
8. print(str[6])
Output:
L
0
Index Error: string index out of range
Here, we must notice that the upper range given in the slice operator is
always exclusive
Consider the following example:
1. # Given String
2. str = "JAVATPOINT"
3. # Start Oth index to end
4. print(str[0:])
5. # Starts 1th index to 4th index
6. print(str[1:5])
7. # Starts 2nd index to 3rd index
8. print(str[2:4])
9. # Starts 0th to 2nd index
10. print(str[:3])
11. #Starts 4th to 6th index
12. print(str[4:7])
Output:
JAVATPOINT
AVAT
VA
JAV
TPO
We can do the negative slicing in the string; it starts from the rightmost
character, which is indicated as -1. The second rightmost index indicates -2, and so
on. Consider the following image.
Reassigning Strings
Updating the content of the strings is as easy as assigning it to a new string.
The string object doesn't support item assignment i.e., A string can only be
replaced with new string since its content cannot be partially replaced.
Strings are immutable in Python.
Example 1
1. str = "HELLO"
2. str[0] = "h"
3. print(str)
Output:
Traceback (most recent call last):
File "[Link]", line 2, in <module>
str[0] = "h";
TypeError: 'str' object does not support item assignment
However, in example 1, the string str can be assigned completely to a new
content as specified in the following example.
Example 2
1. str = "HELLO"
2. print(str)
3. str = "hello"
4. print(str)
Output:
HELLO
hello
Deleting the String
As we know that strings are immutable. We cannot delete or remove the
characters from the string. But we can delete the entire string using
the del keyword.
1. str = "JAVATPOINT"
2. del str[1]
Output:
TypeError: 'str' object doesn't support item deletion
Now we are deleting entire string.
1. str1 = "JAVATPOINT"
2. del str1
3. print(str1)
Output:
NameError: name 'str1' is not defined
String Operators
Operato Description
r
+ It is known as concatenation operator used to join the strings given either side of the
operator.
* It is known as repetition operator. It concatenates the multiple copies of the same
string.
[] It is known as slice operator. It is used to access the sub-strings of a particular string.
[:] It is known as range slice operator. It is used to access the characters from the
specified range.
in It is known as membership operator. It returns if a particular sub-string is present in
the specified string.
not in It is also a membership operator and does the exact reverse of in. It returns true if a
particular substring is not present in the specified string.
r/R It is used to specify the raw string. Raw strings are used in the cases where we need
to print the actual meaning of escape characters such as "C://python". To define any
string as a raw string, the character r or R is followed by the string.
% It is used to perform string formatting. It makes use of the format specifiers used in C
programming like %d or %f to map their values in python.
1. str = "Hello"
2. str1 = " world"
3. print(str*3) # prints HelloHelloHello
4. print(str+str1)# prints Hello world
5. print(str[4]) # prints o
6. print(str[2:4]); # prints ll
7. print('w' in str) # prints false as w is not present in str
8. print('wo' not in str1) # prints false as wo is present in str1.
9. print(r'C://python37') # prints C://python37 as it is written
10. print("The string str : %s"%(str)) # prints The string str : Hello
Output:
HelloHelloHello
Hello world
o
ll
False
False
C://python37
The string str : Hello
COURTESY : [Link]