0% found this document useful (0 votes)
7 views3 pages

String Built-In Function

The document provides a comprehensive overview of various string manipulation functions in Python, including methods like lower(), upper(), count(), find(), and more. Each function is described with its purpose and includes examples demonstrating its usage. This serves as a reference for understanding how to manipulate strings effectively in Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

String Built-In Function

The document provides a comprehensive overview of various string manipulation functions in Python, including methods like lower(), upper(), count(), find(), and more. Each function is described with its purpose and includes examples demonstrating its usage. This serves as a reference for understanding how to manipulate strings effectively in Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Function Description Example

>>> str1 = 'hello WORLD!'


lower() Returns the string with all >>> [Link]()
uppercase letters converted to 'hello world!'
lowercase
>>> str1 = 'hello WORLD!'
upper() Returns the string with all >>> [Link]()
lowercase letters converted to 'HELLO WORLD!'
uppercase
>>> str1 = 'Hello World! Hello
count(str,start,end) Returns number of times Hello'
substring occurs in the string >>> [Link]('Hello',12,25)
2
>>> [Link]('Hello')
3

>>> str1 = 'Hello World! Hello Hello'


find(str,start,end) Returns first index of >>> [Link]('Hello',10,20)
substring, -1 if not found 13
>>> [Link]('Hello',15,25)
19
>>> [Link]('Hello')
0
>>> [Link]('Hee')
-1

>>> str1 = 'Hello World! Hello


index(str,start,end) Same as find() but gives error Hello'
if substring not found >>> [Link]('Hello')
0
>>> [Link]('Hee')
ValueError: substring not found

>>> str1 = 'Hello World!'


endswith() Returns True if string ends >>> [Link]('World!')
with substring True
>>> [Link]('!')
True
>>> [Link]('lde')
False

>>> str1 = 'Hello World!'


startswith() Returns True if string starts >>> [Link]('He')
with substring True
>>> [Link]('Hee')
False

>>> str1 = 'HelloWorld'


isalnum() True if all characters are >>> [Link]()
alphabets or numbers True
>>> str1 = 'HelloWorld2'
>>> [Link]()
True
>>> str1 = 'HelloWorld!!'
>>> [Link]()
False

>>> str1 = 'hello world!'


islower() True if string has at least one >>> [Link]()
lowercase and no uppercase True
>>> str1 = 'hello 1234'
>>> [Link]()
True
>>> str1 = 'hello ??'
>>> [Link]()
True
>>> str1 = '1234'
>>> [Link]()
False
>>> str1 = 'Hello World!'
>>> [Link]()
False

>>> str1 = 'HELLO WORLD!'


isupper() True if string has at least one >>> [Link]()
uppercase and no lowercase True
>>> str1 = 'HELLO 1234'
>>> [Link]()
True
>>> str1 = 'HELLO ??'
>>> [Link]()
True
>>> str1 = '1234'
>>> [Link]()
False
>>> str1 = 'Hello World!'
>>> [Link]()
False

>>> str1 = ' \n \t \r'


isspace() True if string contains only >>> [Link]()
whitespace characters True
>>> str1 = 'Hello \n'
>>> [Link]()
False

>>> str1 = 'Hello World!'


istitle() True if first letter of every >>> [Link]()
word is uppercase True
>>> str1 = 'hello World!'
>>> [Link]()
False

>>> str1 = ' Hello World!'


lstrip() Removes spaces from left >>> [Link]()
side of string 'Hello World! '

>>> str1 = ' Hello World!'


rstrip() Removes spaces from right >>> [Link]()
side of string ' Hello World!'

>>> str1 = ' Hello World!'


strip() Removes spaces from both >>> [Link]()
sides of string 'Hello World!'

>>> str1 = 'Hello World!'


replace(old,new) Replaces all occurrences of >>> [Link]('o','*')
old string with new string 'Hell* W*rld!'
>>> str1 = 'Hello World!'
>>> [Link]('World','Country')
'Hello Country!'
>>> str1 = 'Hello World! Hello'
>>> [Link]('Hello','Bye')
'Bye World! Bye'

>>> str1 = ('HelloWorld!')


join() Joins characters using >>> str2 = '-' #separator
separator >>> [Link](str1)
'H-e-l-l-o-W-o-r-l-d-!'

>>> str1 = 'India is a Great Country'


partition() Splits string into 3 parts based >>> [Link]('is')
on first occurrence of ('India ', 'is', ' a Great
Country')
separator >>> [Link]('are')
('India is a Great Country',' ','
')
>>> str1 = 'India is a Great Country'
split() Splits string into list based on >>> [Link]()
separator ['India','is','a','Great',
'Country']
>>> str1 = 'India is a Great
Country'
>>> [Link]('a')
['Indi', ' is ', ' Gre', 't
Country']

You might also like