Strings in Python
A string in the python is a sequence of the characters. Moreover, a character is just a symbol. For example, the English alphabet has total of 26 characters. Let us explore the topic of creating strings here.
The computer systems do not deal with the characters, however, they basically deal with the numbers i.e. binary. Even though we may see some characters on our screen, it is internally available in the storage as a combination of 0s and 1s.
Moreover, this transformation of the character to a number is known as encoding. In addition, the reverse procedure is termed decoding. Consequently, ASCII and Unicode are 2 of the popular and well-known encodings people use.
In Python, Unicode was introduced so that it can include every character in all the languages and bring evenness in the encoding. Moreover, with the help of the Python Unicode, we can learn about Unicode.
Browse more Topics Under Strings
Creating a String in Python
We can create the strings by enclosing the characters inside one quote or double-quotes too. Moreover, we can even use the triple quotes in Python but usually, we use it to represent the multiline strings as well as the docstrings.
# defining strings in Python
# all of the following are equivalent
my_string = 'Hey'
print(my_string)
my_string = "Hey"
print(my_string)
my_string = '''Hey'''
print(my_string)
# triple quotes string can extend multiple lines
my_string = """Hey, welcome to
the world of Python"""
print(my_string)
When we will run this program, we will be getting this output:
Hey
Hey
Hey
Hey, welcome to
the world of Python
Accessing Characters in a String
We can easily access the individual characters with the use of indexing and a range of the characters with the use of slicing. Moreover, the index begins from ‘0’. However, if we try to access a character out of the index range it will raise an ‘IndexError’. Thus, the index should be an integer. Similarly, we don’t have permission to use floats or any other types, otherwise. Thus, it can result in a TypeError.
Moreover, python permits negative indexing for its sequences.
The index of -1 means the final item, -2 means the second last item and this continues. Further, we can easily access a range of the items in a string with the use of the slicing operator ‘:’ (colon).
#Accessing string characters in Python
str = 'programiz'
print('str = ', str)
#first character
print('str[0] = ', str[0])
#last character
print('str[-1] = ', str[-1])
#slicing 2nd to 5th character
print('str[1:5] = ', str[1:5])
#slicing 6th to 2nd last character
print('str[5:-2] = ', str[5:-2])
When we will run the program present above, we will be getting this output:
str =Â programiz
str[0] =Â p
str[-1] =Â z
str[1:5] =Â rogr
str[5:-2] =Â am
In case we try to access an index out of the range or if we use numbers other than an integer, we will get the errors.
# index must be in range
>>> my_string[15]
...
IndexError: string index out of range
# index must be an integer
>>> my_string[1.5]
...
TypeError: string indices must be integers
Changing or Deleting a String
The strings are immutable in nature. Moreover, this shows that elements of a string are not changeable once we assign them.
>>> my_string = 'programiz'
>>> my_string[5] = 'a'
...
TypeError: 'str' object does not support item assignment
>>> my_string = 'Python'
>>> my_string
'Python'
We aren’t able to delete or remove the characters from a string. However, deleting the string entirely is all way we can do with the use of the ‘del’ keyword.
>>> del my_string[1]
...
TypeError: 'str' object doesn't support item deletion
>>> del my_string
>>> my_string
...
NameError: name 'my_string' is not defined
FAQs on Creating Strings
Question 1: Define the strings in Python.
Answer: A string in the Python is an order of the characters.
Question 2: Define types of strings in Python.
Answer: There are two types of strings in python. Thus, strings that we store as characters and the strings that we store as bytes.
Leave a Reply