String

Strings are generally a sequence of character. It is declared within ” “. If you want to declare with multiple lines of string, then “”” “”” is used.

\n is used to move to the next line. If we have different line \ is used to combine them.

>>> a = ” He started to \n learn python ”

>>> print(a)

He started to

learn python

>>> a = ” He started to \

… learn python ”

>>> print(a)

He started to learn python.

To get the length of the string len() command is used

>>> a = “Python”

>>> len(a)

6

Different ways to handle string

There are many ways to handle a string. The some are.

>>> s = “learn python”

>>> s.title()

Learn Python

To display all contents in Caps, upper() keyword is used. To display all contents in small letter, lower() keyword is used

>>> s = “Python”

>>> s.upper()

PYTHON

>>> s.lower()

python

To change all the capital letters to small and small to capital. swapcase() is used.

>>> s = “PyThOn”

>>> s.swapcase()

pYtHoN

To check the condition, is keyword is used. isalnum() is used to check whether the string contains only alphanumeric or not.

Isalpha() is used to check whether the string contains only aphates. If it contains, then it will return either True or false.

Similarly we can check other conditions like.

>>> s = “1234”

>>> s.isdigit()

True

>>> s = “PYTHON”

>>> s.isupper()

True

>>> s = “Python”

>>> s.islower()

False

Leave a comment