Python Strings

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.

  1. Using single quotes
  2. Using double quotes
  3. Using triple quotes



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.
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.

>>>b="hello world"

>>>print(b[2:5])


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.
For e.g. consider the following example:

>>>b="hello world"

>>>print(b.strip())


The len() method: This method returns us the length of the string that is passed as the parameter to the function.
For e.g. consider the following example:

>>>b="hello world"

>>>print(len(b))


The lower() method: This function returns the string in the lower case letters.
For e.g. let us consider the following example:

>>>b="hello world"

>>>print(b.lower())


The upper() method: This function returns the string in the upper case letters.
For e.g. let us consider the following example:

>>>b="hello world"

>>>print(b.upper())


The replace() method: This method will replace the given string with another string.
For e.g. let us consider the following example:

>>>b="hello world"

>>>print(b.replace("h","j"))


The split() method: This method splits the string into substrings if it finds instances of the separator:

>>>b="hello world"

>>>print(b.split("",""))