Python String
In this section we will discuss one of the most popular data type that is used in python i.e. the string data type. So, let us continue with the study of string data type in python. String is one of the most used data type in python that stores the string type of values in it.
If you are doing programming in python and you want to use the string data type in your program then you can use any one of the three methods to use the string data type.
Let us consider the following example to create a string in python:
>>>b="hello world" >>>print(b)
In python programming language, the character data type is not supported i.e. the string in python is treated as the sequence of strings in python. For e.g. let us consider the character 'p' then it treated as a string of length 1 in python. So, in this way the strings are treated in python.
Strings are Arrays
Just like any programming language , in python also the strings are also treated as the arrays of bytes that represent the unicode characters. So, in this way the strings are arrays.
However as we now know that the python language does not support the character data type i.e. a chacter is simply treated as a string of length 1. But, we can access the elements of the string with the use of the square brackets. For e.g. if we have a string a and we want to access it's elements then they can be accessed like a[i], where i is the index value of the elements which you want to access.
>>>b="hello world" >>>print(b[1])
Common functions used with the string data type
Substring. Get the characters from initial position to final position where the final position is not included in the result. >>>b="hello world" >>>print(b[2:5])
For e.g. consider the following example in which we want to get the substrings between 2 to 5 and the element at 5th position is not included in the final result.
The strip() method: it removes the white spaces that are present at the beginning or at the ending of any string. So, it gives us a string without any spaces at the beginning or ending of the string. >>>b="hello world" >>>print(b.strip())
For e.g. consider the following example:
The len() method: This method returns us the length of the string that is passed as the parameter to the function. >>>b="hello world" >>>print(len(b))
For e.g. consider the following example:
The lower() method: This function returns the string in the lower case letters. >>>b="hello world" >>>print(b.lower())
For e.g. let us consider the following example:
The upper() method: This function returns the string in the upper case letters. >>>b="hello world" >>>print(b.upper())
For e.g. let us consider the following example:
The replace() method: This method will replace the given string with another string. >>>b="hello world" >>>print(b.replace("h","j"))
For e.g. let us consider the following example:
The split() method: This method splits the string into substrings if it finds instances of the separator:
>>>b="hello world" >>>print(b.split("",""))