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)
String variable declaration and assigning:
name = "obula"
age = "20"
# here both are strings because values defined in quotes
print(name)
print(age)
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))
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)
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))
String Replication:
It means printing the same string value as many times as you want.
name = "obula"
print(name * 3)
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])
String Capitalize:
First letter capitalized in output.
name = "obula"
print(name.capitalize())
String Upper:
It converts all text into uppercase.
name = "obula"
print(name.upper())
String Lower:
It converts all text into lowercase.
name = "OBuLa"
print(name.lower())
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))
String Find:
Find gives the index number from where the substring starts.
name = "obulainformative"
fin = "info"
print(name.find(fin))
String Index:
It prints the starting index number.
name = "obulainformative"
ind = "info"
print(name.index(ind))
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())
String isalpha:
name = "obula"
name1 = "obula12"
print(name.isalpha())
print(name1.isalpha())
String isdigit:
It checks if all are digits.
name = "obula"
phone = "12345"
print(name.isdigit())
print(phone.isdigit())
String islower:
It checks if all letters are lowercase.
name = "obula"
name1 = "ObulA"
print(name.islower())
print(name1.islower())
String isupper:
It checks if all letters are uppercase.
name = "OBULA"
name1 = "OBULa"
print(name.isupper())
print(name1.isupper())
String Strip:
It removes all whitespaces at the start and end of a string.
name = " obula informative "
print(name.strip())
String Replace:
It replaces one letter with another.
name = "obula informative"
print(name.replace("a", "e"))
String split:
It splits a string into substrings based on any separator and returns a list.
name = "obula,informative"
print(name.split(','))
Comments
Post a Comment