Welcome to another tutorial, here you learn about Built –the string function in Python. You will get to know some of the string functions.
The len function also, known as the length function can be used to determine the character length of any string. It typically returns a number and it takes a string as an argument. Check out the example below:
>>> s = "Hello"
>>> print (len(s))
Output:
5
The find function is used to locate the position of any character or a subString within any given string. However, the find functions implementation is quite different from the normal function, but it is not difficult to understand.
To find a substring in any string, you first have to provide both the main string and the substring to be found to the function. Check out the example below:
>>> s = "Hello"
>>> ss = "He"
>>> print (s.find(ss))
Output:
0
Since, He is present at the beginning of the string Hello, hence index 0 is returned as the result. Another example to use find() method in one line.
>>> print ("Hello".find("He"))
Output:
0
The lower() function in Python, is typically used to convert all the uppercase characters existing in a string into lowercase. Also, the lower() function accepts a string as the function input, but the string is not passed as an argument, and it returns a string at the end.
>>> print ("Hello, World".lower());
Output:
hello world
In Python, the upper() is typically used to turn all the characters in a string to uppercase.
>>> print ("Hello, World".upper());
Output:
HELLO WORLD
In Python, the islower() is typically used to check if string.the name string is in lowercase or not. It returns a boolean value as result, either as true or false.
>>> print ("hello, world".islower())
Output:
True
Another
>>> print ("Hello, World".islower());
Output:
False
In Python, the isupper() is typically used to check if the given string is in uppercase or not. It, however, returns a boolean value as result, that is, either True or False.
>>> print ("HELLO, WORLD".isupper());
Output:
True
Another Example
>>> print ("Hello, World".isupper());
Output:
False
In Python, the replace() will take a string as input at first, then request for some subString within it as the first argument and ask for another string to replace that subString as the second argument. Check out the example below:
>>> print ("Hello, World".replace("World", "Python"));
Output:
Hello Python
for instance, you are having a string say:
>>> mystring = "Hello World! Welcome to the Python tutorial"
As shown above, we can then use the split() function to split the above-declared string.
Suppose, we decide to split the string into two substrings from the exclamation mark '!' in the character argument, then it will split it into parts depending upon the number of exclamation marks '!' in the string. Also, every sub-pieces of the string will be stored in a list as shown below:
>>> print (mystring.split("!"))
Output:
['Hello World', ' Welcome to the Python tutorial']
In addition, these values can be stored in another variable and each element can be accessed shown below:
>>> myNEWstring = mystring.split("!")
>>> print (myNEWstring[0]);
>>> print (myNEWstring[1]);
Output:
Hello World
Welcome to the Python tutorial