Strings in Python

String is nothing but a group of characters defined inside quotes. Python does not support character data type. String takes values or text in double quotes or single quotes.


first = "Hello world"
# or
first = 'Hello world'
print(first)
OUTPUT:
Hello world

String variable declaration and assigning:


name = "obula"
age = "20"
# here both are strings because values defined in quotes
print(name)
print(age)
OUTPUT:
obula
20

String Length Function:

In Python, we have len() to find the length of a string.


name = "obula"
age = "20"
print(len(name))
print(len(age))
OUTPUT:
5
2

String Concatenation:

String concatenation will be done by using the + operator. Concatenation means adding two strings.


name = "obula"
age = "20"
# concatenation
print(name + age)
# with space
print(name + " " + age)
OUTPUT:
obula20
obula 20

String Concatenation with Number:

String cannot be concatenated with a number; it will throw an error type str, not int.


name = "obula"
age = 20
print(name + age)  # Error

By using the format method, we can add string and integer values:


name = "obula {}"
age = 20
print(name.format(age))
OUTPUT:
obula 20

String Replication:

It means printing the same string value as many times as you want.


name = "obula"
print(name * 3)
OUTPUT:
obulaobulaobula

String Slicing:

Slicing means extracting a particular part of a string. Indexing starts from zero.


name = "obulainformative"
print(len(name))
print(name[3:7])
print(name[3:])
print(name[:5])
print(name[-6:-3])
OUTPUT:
16
lain
lainformative
obula
mat

String Capitalize:

First letter capitalized in output.


name = "obula"
print(name.capitalize())
OUTPUT:
Obula

String Upper:

It converts all text into uppercase.


name = "obula"
print(name.upper())
OUTPUT:
OBULA

String Lower:

It converts all text into lowercase.


name = "OBuLa"
print(name.lower())
OUTPUT:
obula

String Count:

It counts how many times a letter or word is repeated in a string.


name = "obulainformative"
rep = "a"
print(name.count(rep))
name = "obula info obula"
rep = "obula"
print(name.count(rep))
OUTPUT:
2
2

String Find:

Find gives the index number from where the substring starts.


name = "obulainformative"
fin = "info"
print(name.find(fin))
OUTPUT:
5

String Index:

It prints the starting index number.


name = "obulainformative"
ind = "info"
print(name.index(ind))
OUTPUT:
4

String Alphanumeric:

It checks if the string is alphanumeric or not.


name = "obula"
print(name.isalnum())
name = "obula12"
print(name.isalnum())
name = "obula@12"
print(name.isalnum())
name = "12345"
print(name.isalnum())
OUTPUT:
True
True
False
True

String isalpha:


name = "obula"
name1 = "obula12"
print(name.isalpha())
print(name1.isalpha())
OUTPUT:
True
False

String isdigit:

It checks if all are digits.


name = "obula"
phone = "12345"
print(name.isdigit())
print(phone.isdigit())
OUTPUT:
False
True

String islower:

It checks if all letters are lowercase.


name = "obula"
name1 = "ObulA"
print(name.islower())
print(name1.islower())
OUTPUT:
True
False

String isupper:

It checks if all letters are uppercase.


name = "OBULA"
name1 = "OBULa"
print(name.isupper())
print(name1.isupper())
OUTPUT:
True
False

String Strip:

It removes all whitespaces at the start and end of a string.


name = "  obula informative  "
print(name.strip())
OUTPUT:
obula informative

String Replace:

It replaces one letter with another.


name = "obula informative"
print(name.replace("a", "e"))
OUTPUT:
obule informetive

String split:

It splits a string into substrings based on any separator and returns a list.


name = "obula,informative"
print(name.split(','))
OUTPUT:
['obula', 'informative']


Comments