Python String Methods

Vipin | Sun, 14 Jun, 2020 | 221

String Methods in Python

Python has a set of built-in methods that you can be used for string manipulations.

strip()

It remove both the leading and trailing whitespaces.

Example:-

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

lower()

The lower() method returns the string in lower case.

Example:-

a = "Hello, World!"
print(a.lower()) 

upper()

The upper() method returns the string in upper case.

Example:-

a = "Hello, World!"
print(a.upper())

replace()

The replace() method replaces a string with another string.

Example:-

a = "Hello, World!"
print(a.replace("H", "J"))

split()

The split() method splits the string into substrings if it finds instances of the separator.

Example:-

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

Check a String in another String

To check if a certain phrase or character is present in a string, we can use the keywords in or not in.

Example

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)

Example

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) 

String Concatenation

To concatenate, or combine, two strings you can use the + operator.

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

For other string methods find in the given reference.

0 comments
Leave a comment