Vipin | Sun, 14 Jun, 2020 | 221
Python has a set of built-in methods that you can be used for string manipulations.
It remove both the leading and trailing whitespaces.
a = " Hello, World! " print(a.strip()) # returns "Hello, World!"
lower() method returns the string in lower case.
a = "Hello, World!" print(a.lower())
upper() method returns the string in upper case.
a = "Hello, World!" print(a.upper())
replace() method replaces a string with another string.
a = "Hello, World!" print(a.replace("H", "J"))
split() method splits the string into substrings if it finds instances of the separator.
a = "Hello, World!" print(a.split(",")) # returns ['Hello', ' World!']
capitalize() – Returns the string with the first character capitalized and rest of the characters in lower case.
var = 'PYTHON' print (var.capitalize()) # Python
swapcase() – Swaps the case of every character in the String means that lowercase characters got converted to uppercase and vice-versa.
var = 'TechBeamers' print (var.swapcase()) # tECHbEAMERS
title() – Returns the ‘titlecased’ version of String, which means that all words start with uppercase and the rest of the characters in words are in lowercase.
var = 'welcome to Python programming' print (var.title()) # Welcome To Python Programming
count( str[, beg [, end]]) – Returns the number of times substring ‘str’ occurs in the range [beg, end] if beg and end index are given else the search continues in full String Search is case-sensitive.
var='TechBeamers' str='e' print (var.count(str)) # 3 var1='Eagle Eyes' print (var1.count('e')) # 2 var2='Eagle Eyes' print (var2.count('E',0,5)) # 1
islower() – Returns ‘True’ if all the characters in the String are in lowercase. If any of the char is in uppercase, it will return False.
var='Python' print (var.islower()) # False var='python' print (var.islower()) # True
isupper() – Returns ‘True’ if all the characters in the String are in uppercase. If any of the char is in lowercase, it will return False.
var='Python' print (var.isupper()) # False var='PYTHON' print (var.isupper()) # True
isdecimal() – Returns ‘True’ if all the characters in String are decimal. If any character in the String is of other data-type, it will return False.
Decimal characters are those from the Unicode category Nd.
num=u'2016' print (num.isdecimal()) # True
isdigit() – Returns ‘True’ for any char for which isdecimal() would return ‘True and some characters in the ‘No’ category. If there are any characters other than these, it will return False’.
Precisely, digits are the characters for which Unicode property includes: Numeric_Type=Digit or Numeric_Type=Decimal.
For example, superscripts are digits, but fractions not.
print ('2'.isdigit()) # True print ('²'.isdigit()) # True
To check if a certain phrase or character is present in a string, we can use the keywords
Check if the phrase "ain" is present in the following text:
txt = "The rain in Spain stays mainly in the plain" x = "ain" in txt print(x)
Check if the phrase "ain" is NOT present in the following text:
txt = "The rain in Spain stays mainly in the plain" x = "ain" not in txt print(x)
To concatenate, or combine, two strings you can use the + operator.
a with variable
b into variable
a = "Hello" b = "World" c = a + b print(c)