Built-in string operations in Python -II

In built string functions

This tutorial is a continuation of the last tutorial on inbuilt string functions. Let’s have a look at some more interesting inbuilt string functions.

How to determine is a string ends with a particular suffix

If you want to know whether a string ends in a particular suffix or not then you will find endswith() function very useful. Have a look at the following piece of code. This function takes three parameters- (1) the substring that you will look for at the end,(2) the start point and (3) the end point for the search.

var1 = 'Apple'
if var1.endswith('ple',0,len(var1)):
    print('"Apple" ends with "ple"')

Download the code Run the code

Check whether a string is alphanumeric or not

A string is alpha numeric if it only consists of alphabets or numbers. The function isalnum() is used to identify whether a string is alphanumeric or not. If yes, it returns true else false.

 

var1 = 'Ap**ple'
if var1.isalnum():
    print(var1 + 'is Alpha-numeric')
else: print(var1 + ' is not Alpha-numeric')

Download the code Run the code

Check whether a string is purely alphabets or not

If you want to verify whether a string is purely alphabets or not then you can use the function isalpha(). If yes, it returns true else false.

 

var1 = 'Apple'
if var1.isalpha():
    print(var1 + ' is pure alphabets')
else: print(var1 + ' is not pure alphabets')

Download the code Run the code

Check whether a string is purely digits or not

If you want to verify whether a string is purely digits or not then you can use the function isdigit(). If yes, it returns true else false.

 

var1 = '12xyz'
if var1.isdigit():
    print(var1 + ' is pure digits')
else: print(var1 + ' is not pure digits')

Download the code Run the code

Check whether a string is purely small alphabets or not

In order to check whether a string is purely lower case or not then you can use the function islower(). If the string is all lower alphabets it returns true else false.

 

var1 = 'apple'
if var1.islower():
    print(var1 + ' is lower')
else: print(var1 + ' is not lower')

Download the code Run the code

Check whether a string is purely capital alphabets or not

In order to check whether a string is purely upper case or not you can use the function isupper(). If the string is all upper alphabets it returns true else false.

var1 = 'APPLE'
if var1.isupper():
    print(var1 + ' is upper')
else: print(var1 + ' is not upper')

Download the code Run the code

<<< Built-in string operations in PythonString Formatting Operators >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .