A String is a combination of sequence characters. Strings can be created in Python by enclosing them around quotes. Python is designed in such a way that single or double quotes are one and the same, both work in similar way. The index of the string starts from Zero.
Example :
Str1 = “Hello python!!”
Str2 = ‘Python string example’
- len(String) : Length method will return the string length count.
Example :
1 2 3 |
str = "python language" Print(len(str)) |
Here the length of the string “Python language” is 15 because index of the string
Starts with zero.
- lower() : Converts the complete string into lower case.
Example :
1 2 3 |
str = "PYTHON language" Print(str.lower()) |
Here the string “PYTHON language” is converted to lower case irrespective of the current case.
3.Upper() : Converts the complete string into upper case.
Example :
1 2 3 |
str = "python langUAGE" Print(str.upper()) |
str = “python langUAGE”
Print(str.upper())
Here the string “Python language” is converted to upper case irrespective of the current case.
4.Replace() : Replace method will replace the existing text with the new text as specified.
1 2 3 |
a = “sam1234sam4321” Print(a.replace(“sam”,”john”)) |
Example : a = “sam1234sam4321”
Print(a.replace(“sam”,”john”))
Here the string “sam1234sam4321” is converting the “sam” text occurence to “John” in the string.
5.Isdigit() : Isdigit method will return true, if the string contains numbers or else will return false.
Example :
1 2 3 |
c = “293944” Print (c.isdigit()) |
Here is the string c contains only numbers the output will be true or else the output will be false.
6.Isalpha() : isalpha method will return true, if the string contains only alphabets without spaces and digits.
Example :
1 2 3 4 5 6 7 |
d = “abcd” Print(d.isalpha()) e = “My name is” print(e.isalpha()) |
First example will return true and second example will return false.