what is Strings in python

0
226
STRING IN PYTHON

 

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’

1. len(String) : Length method will return the string length count.

Example :  str = “python language”

Print(len(str))

Here the length of the string “Python language” is 15 because index of the string

Starts with zero.

2. lower() : Converts the complete string into lower case.

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

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 : 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 : d = “abcd”

Print(d.isalpha())

e = “My name is”

print(e.isalpha())

First example will return true and second example will return false.

LEAVE A REPLY

Please enter your comment!
Please enter your name here