capitalize
- The function capitalize( ) returns a string with first letter capitalized.
- capitalize( ) function does not take any parameter
- If the first letter of the string is an upper case letter or a non-alphabetic character, it returns the original string
Syntax
string.capitalize( )
Example
str1 = "hello all"
print(str1.capitalize())
Output
Hello all
center
- The center( ) method returns a string padded with specified fillchar
- It does not modify the original string and default filler is a space
Syntax
string.center(width,[fillchar])
Example
str1 = "hello all"
str1.center(20,'*')
Output
'*****hello all******'
casefold
The casefold( ) method removes all case distinctions present in a string
Syntax
string.casefold( )
Example
a = "Hello All"
a.casefold( )
Output
'hello all'
count
The count( ) method returns the number of occurences of substring in the range [start, end]
Syntax
string.count(substring, start, end)
Example
str = "This is book"
substr = "i"
str.count(substr)
str.count(substr,3)
substr = "o"
str.count(substr,0,4)
Output
2
1
0
endswith
 The endswith( ) method returns True if a string ends with the specified suffix otherwise return false
Syntax
string.endwith(suffix, start,end)
Example
Text = "Python is very easy to learn"
result = Text.endswith('to learn')
print("check for to learn:",result)
result = Text.endswith('n')
print("check for n:",result)
result = Text.endswith('easy',6,13)
print("check for easy at start = 6 and end = 13:",result)
result=Text.endswith('easy',15,19)
print("check for easy at start = 15 and end = 19:",result)
Output
check for to learn: True
check for n: True
check for easy at start = 6 and end = 13: False
check for easy at start = 15 and end = 19: True
startswith
 The startswith( ) method returns true if a string starts with the specified prefix else it returns false
Syntax
string.startswith(prefix, start,end)
Example
Text = "Python is very easy to learn"
result = Text.startswith('to learn')
print("check for to learn:",result)
result = Text.startswith('python')
print("check for python:",result)
Output
check for to learn: False
check for python: False
upper
 The upper( ) converts all uppercase characters in a string into lowercase characters.
Syntax
string.upper( )
Example
str ="python is easy to learn"
print(str.upper())
Output
PYTHON IS EASY TO LEARN
lower
 The lower( ) converts all lowercase characters in a string into uppercase characters
Syntax
string.lower( )
Example
str ="PYTHON IS EASY TO LEARN"
print(str.lower())
title
The title( ) method converts the string to title case i.e. first characters of all the words are capitalized
Syntax
string.title( )
Example
str = "hello all"
print(str.title( ))
Output
Hello All
swapcase
The swapcase( ) method convers the lower case to upper case and vice versa
Syntax
string.swapcase( )
Example
str = "Hello All"
print(str.swapcase())
Output
hELLO aLL
split
The split( ) method breaks up a string at the specified separator and returns a list of string
Syntax
string.split( [separator],[maxsplit])
Example
str = "python is easy to learn"
str1 = "python,easy,learn"
print(str.split())
print(str1.split(','))
Output
['python', 'is', 'easy', 'to', 'learn']
['python', 'easy', 'learn']
replace
- The replace( ) method search for specified string and replaces it with new string value.
- If the old string is not found, it returns the copy of the original string
Syntax
string.replace( old,new)
Example
person = "very, very intelligent"
print(person.replace("very","good"))
Output
good, good intelligent
join
The join( ) method returns a string concatenated with the elements of an iterable
Syntax
string.join(iterable)
Example
str1 = "Python"
a="*"
string1 = "abc"
string2 = "521"
print(a.join(str1))
print(string1.join(string2))
Output
P*y*t*h*o*n
5abc2abc1
isupper
The isupper( ) method returns true if all the characters in the given string is in upper case else it returns false
Syntax
string.isupper( )
Example
str1 = "Python"
str2 = "PYTHON"
print("Python:",str1.isupper())
print("PYTHON:",str2.isupper())
Output
Python: False
PYTHON: True
islower
The islower( ) method returns true if all the characters in the given string is in lower case else it returns false
Syntax
string.islower( )
Example
str1 = "Python"
str2 = "python"
print("Python:",str1.islower())
print("PYTHON:",str2.islower())
Output
Python: False
PYTHON: True
isalpha
The isalpha( ) returns True if all the characters in the string are alphabets otherwise it returns False
Syntax
string.isalpha( )
Example
str1 = "Python"
str2 = "GE8151python"
str3 = "Python is easy"
print("Python:",str1.isalpha())
print("PYTHON:",str2.isalpha())
print("Python is easy:",str3.isalpha())
Output
Python: True
PYTHON: False
Python is easy: False
isalnum
The isalnum( ) method returns True if all the characters in the string are alphanumeric, otherwise it returns false
Syntax
string.isalnum( )
Example
str1 = "Python"
str2 = "GE8151python"
str3 = "Python is easy"
print("Python:",str1.isalnum())
print("PYTHON:",str2.isalnum())
print("Python is easy:",str3.isalnum())
Output
Python: True
PYTHON: True
Python is easy: False
isdigit
The isdigit( ) method returns true if all characters in a string are digits, otherwise it returns false
Syntax
string.isdigit( )
Example
str1 = "Python"
str2 = "8151"
print("Python:",str1.isdigit())
print("8151:",str2.isdigit())
Output
Python: False
8151: True
isspace
The isspace( ) method checks whether the string consists of white space only
Syntax
string.isspace( )
Example
str1 = "Python"
str2 = "GE8151python"
str3 = "Python is easy"
str4 = " "
print("Python:",str1.isspace())
print("PYTHON:",str2.isspace())
print("Python is easy:",str3.isspace())
print(" :",str4.isspace())
Output
Python: False
PYTHON: False
Python is easy: False
: True
istitle
The istitle( ) method returns True if the string is a titlecased string, otherwise returns False
Syntax
string.istitle( )
Example
str1 = "Python"
str2 = "GE8151 python"
str3 = "Python Is Easy"
print("Python:",str1.istitle())
print("GE8151 python:",str2.istitle())
print("Python Is Easy:",str3.istitle())
Output
Python: True
GE8151 python: False
Python Is Easy: True
find
The find( ) method returns index of substring, if it is found. Otherwise -1 is returned
Syntax
string.find( )
Example
str1 = "Python is easy"
print("is:",str1.find("is"))
print("fun:",str1.find("fun"))
Output
is: 7
fun: -1
len
The len( ) method returns the length of the string
Syntax
len(string)
Example
str1 = "python"
print("Length of python:",len(str1))
Output
Length of python: 6
min
The min( ) method returns the minimum character in the string
Syntax
min(string)
Example
str1 = "python"
print("Minimum character of python:",min(str1))
Output
Minimum character of python: h
max
The max( ) method returns the maximum character in the string
Syntax
max(string)
Example
str1 = "python"
print("Maximum character of python:",max(str1))
Output
Maximum character of python: y
String Module
- A module is a file containing python definitions, functions, statements
- Standard library of python is extended as modules
- To use these modules in a program, programmer needs to import the module
- Once the module is imported, the functions or variables are referenced in the code
- There is large number of standard modules also available in python
- Standard modules are imported in the same way as user defined modules are imported
Syntax
import module_name
Example,
import string
print(string.punctuation)
print(string.digits)
print(string.printable)
print(string.capwords("Learn Python"))
print(string.hexdigits)
print(string.octdigits)
Output
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
0123456789
123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
Learn Python
0123456789abcdefABCDEF
01234567
Escape sequences in strings
Views: 0