A string is a sequence of characters. A character is simply a symbol. For example, the English language has 26 characters.
Computers do not deal with characters, they deal with numbers (binary). Even though you
may see characters on your screen, internally so, it is store and manipulate as a combination
of 0’s and 1’s.
Then, This conversion of a character to a number is call encoding, and the reverse process is
decoding. Moreover, ASCII and Unicode are some of the popular encoding used.
So, In Python, the string is a sequence of Unicode characters. After that Unicode was introduce to include every character in all languages and bring uniformity in encoding.
How to create a string in Python?
So, Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.
- So, Strings can be output to the screen using the print function. For example print(“hello”).
- Moreover, Like many other popular programming languages, strings in Python are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of Square brackets can be used to access elements of the string.
How to access characters in a String in python?
- So, We can access individual characters using indexing and a range of characters using
- Moreover, Index starts from 0.
- Then, After Trying to access a character out of the index range will raise an Index Error.
- The index must be an integer.
- Then, We can’t use float or other types, this will result in TypeError.
- So, Python allows negative indexing for its sequences.
- The index of -1 refers to the last item, -2 to the second last item, and so on.
- Then, We can access a range of items in a string by using the slicing operator (colon).
- At last, Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods.
Go for online python compiler and you can compile it
# index must be in range >>> my_string ... IndexError: string index out of range # index must be an integer >>> my_string[1.5] ... TypeError: string indices must be integers
So, Slicing can be best visualized by considering the index to be between the elements as shown below. Then If we want to access a range, we need the index that will slice the portion from the string.
How to change or delete a string?
So, Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. Then, We can simply reassign different strings to the same name.
>>> my_string = 'programiz' >>> my_string = 'a' ... TypeError: 'str' object does not support item assignment >>> my_string = 'Python'>>> my_string 'Python'
So, We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del.
>>> del my_string ... TypeError: 'str' object doesn't support item deletion >>> del my_string >>> my_string ... NameError: name 'my_string' is not defined
So, thank you for reading thing article. In next we have to discuss different types of string in python.
also for more studies visit our website- learning points – Home | A better learning future starts here