0% found this document useful (0 votes)
94 views

Python String Functions

This document describes various string functions in Python. It lists the function name, description, and usage for common string methods like capitalize(), lower(), upper(), find(), replace(), split(), and more. These functions allow users to manipulate and analyze string data by changing case, searching for substrings, counting occurrences, extracting substrings, and other string operations in a Python program.

Uploaded by

Lorraine Liu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Python String Functions

This document describes various string functions in Python. It lists the function name, description, and usage for common string methods like capitalize(), lower(), upper(), find(), replace(), split(), and more. These functions allow users to manipulate and analyze string data by changing case, searching for substrings, counting occurrences, extracting substrings, and other string operations in a Python program.

Uploaded by

Lorraine Liu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python string functions

Function Name Description


capitalize() Returns the String with first character capitalized and rest of the characters in lower case.
lower() Converts all the characters of the String to lowercase.
upper() Converts all the characters of the String to uppercase.
count( str[,beg,end]]) Returns the number of times substring ‘str’ occurs in range [beg, end] if beg and end index are
given. If it is not given then substring is searched in the whole String. Search is case-sensitive.
islower() Returns ‘True’ if all the characters in the String are in lowercase. If any one character is in
uppercase it will return ‘False’.
isupper() Returns ‘True’ if all the characters in the String are in uppercase. If any one character is in
lowercase it will return ‘False’.
isdecimal() Returns ‘True’ if all the characters in String are decimal. If anyone character in the String is of
other data-type, it will return ‘False’.
isdigit() Returns ‘True’ for any character for which isdecimal() would return ‘True and some characters
in ‘No’ category. If there are any characters other than these, it will return ‘False’.
isalpha() Returns ‘True’ if String contains at least one character (non-empty String) and all the
characters are alphabetic, ‘False’ otherwise.
isalnum() Returns ‘True’ if String contains at least one character (non-empty String) and all the
characters are either alphabetic or decimal digits, ‘False’ otherwise.
find(str [,i [,j]]) Searches for ‘str’ in complete String (if i and j not defined) or in a sub-string of String (if i and
j are defined).This function returns the index if ‘str’ is found else returns ‘-1’, where, i=search
starts from this index. j=search ends at this index.
index(str[,i [,j]]) This is same as ‘find’ method. The only difference is that it raises ‘ValueError’ exception if
‘str’ is not found.
rfind(str[,i [,j]]) This is same as find() just that this function returns the last index where ‘str’ is found. If ‘str’ is
not found it returns ‘-1’.
count(str[,i [,j]]) Returns the number of occurrences of substring ‘str’ in the String. Searches for ‘str’ in
complete String (if i and j not defined) or in a sub-string of String (if i and j are defined),
where, i=search starts from this index, j=search ends at this index.
replace(old,new[,count]) Replaces all the occurrences of substring ‘old’ with ‘new’ in the String.
If ‘count’ is defined then only ‘count’ number of occurrences of ‘old’ will be replaced with
‘new’, where, old =substring to be replaced, new =substring that will replace the old, count
=number of occurrences of old that will be replaced with new.
split([sep[,maxsplit]]) Returns a list of substring obtained after splitting the String with ‘sep’ as delimiter, where, sep=
delimiter, default is space, maxsplit= number of splits to be done.
lstrip([chars]) Returns a String after removing the characters from the beginning of the String.
where,
Chars=this is the character to be trimmed from the String. Default is whitespace character.
rstrip() Returns a String after removing the characters from the End of the String.
where, Chars=this is the character to be trimmed from the String. Default is whitespace
character.
len(string) Returns the length of given String

CS1026 Midterm Exam – October 2018 Page 15 of 16


 

You might also like