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

st fns

The document provides an overview of various string functions available in Python, including len(), count(), find(), index(), replace(), split(), capitalize(), lower(), upper(), title(), strip(), lstrip(), rstrip(), join(), ljust(), rjust(), and center(). Each function is explained with its syntax and examples demonstrating its usage. These functions allow for string manipulation without altering the original string.

Uploaded by

shroffyohann2
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)
4 views

st fns

The document provides an overview of various string functions available in Python, including len(), count(), find(), index(), replace(), split(), capitalize(), lower(), upper(), title(), strip(), lstrip(), rstrip(), join(), ljust(), rjust(), and center(). Each function is explained with its syntax and examples demonstrating its usage. These functions allow for string manipulation without altering the original string.

Uploaded by

shroffyohann2
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
You are on page 1/ 29

Functions in Strings

String Functions
• Python has a number of string functions which are in the string library.

• These functions are already built into every string - we invoke them by
appending the function to the string variable.

• These functions do not modify the original string, instead they return a
new string that has been altered.
Length function
• The len() function is used to find the length of a string including
spaces.
>>> word=‘hello world’
>>>len(word)
11

>>>word=‘\new text’
>>>len(word)
8

>>>word=r‘\new text’
>>>len(word)
9
Count function
• The count() function is used to find number of times a character or
word is repeated in a string.
>>> s='hello world'
>>> s.count('l')
3
>>> s='hi hi hi hello'
>>> s.count(' ')
3
>>> s.count('hi')
3
Find function
• The find() function is used to find the given character in a string. It
returns the position of the character if it finds.
>>> s='hello world'
>>> s.find('e')
1
>>> s.find(' ')
5
>>> s.find('hello')
0
>>> s1='hi hi'
>>> s1.find(' hi')
2
>>> s1.find('Hi')
-1
Index function
• The index() function is similar to find as it is used to find the given
character/word in a string. It returns the position of the character if it
finds. The index() function allows us to use index values to find out the
specified character or word in a string.
>>> s='hi Hello hi Hello' >>> s.index('hi')
>>> s.index('i') 0
1 >>> s.index('hi',5)
>>> s.index('H',0,5) 9
3 >>> s.index('l',8,-2)
>>> s.index('H',8) 14
12
Replace function
• The replace() function replaces a search character/word with specified
character or word. The syntax is

string name.replace(old value, new valule, count)

Parameter Description
old value Required. The string to search for
new value Required. The string to replace the old value with
count Optional. A number specifying how many occurrences of the old
value you want to replace. Default is all occurrences
Replace function
>>> s='hi hi hi hi'
>>> s.replace('hi', 'hello')
'hello hello hello hello'

>>> s
'hi hi hi hi'

>>> s.replace('hi','hello',1)
'hello hi hi hi'
>>> s.replace('hi','hello',2)
'hello hello hi hi'
>>> s.replace('Hi','Hello')
'hi hi hi hi'
Split function
• The split() function breaks the string based on the separator and
returns a list of strings where each string is a list item. The syntax is

string name.split(separator, maxsplit)

Parameter Description
separator Optional. Specifies the separator to use when splitting the string. By
default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which
is "all occurrences"
Split function
>>> s='My name is srinivas'
>>> s.split()
['My', 'name', 'is', 'srinivas']

>>> s.split('i',1)
['My name ', 's srinivas']
>>> s.split('i',2)
['My name ', 's sr', 'nivas']

>>> s1='hi#hello#hi#hello'
>>> s1.split('#')
['hi', 'hello', 'hi', 'hello']
Capitalize function
• The capitalize() function converts the first character of a string to
capital (uppercase) letter and lowercases all other characters. If the
string has its first character as capital, then it returns the original
string. The syntax is

string name.capitalize()

The capitalize function does not take any parameters and returns a
string with the first character capitalized and all other characters lower
cased. It doesn’t modify the original string.
Capitalize function
>>> s='python Programming‘

>>> s.capitalize()
'Python programming‘

>>> s='Python ProgramminG'


>>> s.capitalize()
'Python programming‘

>>> s='3.8 Version Python'


>>> s.capitalize()
'3.8 version python'
Lower function
• The lower() function converts all uppercase characters in a string into
lowercase characters and returns it. If no uppercase characters exist, it
returns the original string. The syntax is

string name.lower()

The lower() function does not take any parameters.

>>> s='This Is lower case'


>>> s.lower()
'this is lower case'
Upper function
• The upper() function converts all lowercase characters in a string into
uppercase characters and returns it. If no lowercase characters exist, it
returns the original string. The syntax is

string name.upper()

The upper() function does not take any parameters.

>>> s='This Is upper Case'


>>> s.upper()
'THIS IS UPPER CASE'
Title function
• The title() function returns a string with first letter of each word
capitalized; a title cased string. Meaning, the first character of each
word is capitalized (if the first character is a letter). The syntax is

string name.title()

The title() function does not take any parameters. If the word contains
a number or a symbol, the first letter after that will be converted to
upper case.
>>> s='this is title case' >>> s='It is 2nd number'
>>> s.title() >>> s.title()
'This Is Title Case' 'It Is 2Nd Number'
Strip function
• The strip() function removes characters from both left and right based
on the argument (a string specifying the set of characters to be
removed) and returns a copy of the string by removing both the leading
and the trailing characters. The syntax is

string name.strip(characters)

characters - a string specifying the set of characters to be removed


from the left and right part of the string. If the chars argument is not
provided, all leading and trailing whitespaces are removed from the
string.
Strip function
>>> s=' Hello World ' >>> s='Hello World'
>>> s >>> s.strip('d')
' Hello World ' 'Hello Worl'
>>> s.strip() >>> s.strip('ld')
'Hello World‘ 'Hello Wor'
>>> s.strip('H') >>> s.strip('World')
' Hello World ‘ 'Hello '
>>> s='Hello World' >>> s.strip('dl')
>>> s.strip('e') 'Hello Wor'
'Hello World' >>> s.strip('rl')
>>> s.strip('H') 'Hello World'
'ello World' >>> s.strip('rd')
>>> s.strip('He') 'Hello Worl'
'llo World' >>> s=‘$$Hello world$$$’
>>> s.strip(‘$’)
'Hello World‘
Lstrip function
• The lstrip() function removes characters from left based on the
argument (a string specifying the set of characters to be removed) and
returns a copy of the string by removing the leading characters. The
syntax is

string name.lstrip(characters)

characters - a string specifying the set of characters to be removed


from the left of the string. If the chars argument is not
provided, all leading whitespaces are removed from the
string.
Lstrip function
>>> s=' hello world ' >>> s='$$hello$$$'
>>> s >>> s.lstrip('$')
' hello world ' 'hello$$$‘
>>> s.lstrip() >>> s.lstrip('$e')
'hello world ' 'hello$$$'
>>> s.lstrip('h')
' hello world '
>>> s='hello world'
>>> s.lstrip('h')
'ello world'
>>> s.lstrip('eh')
'llo world'
>>> s.lstrip('d')
'hello world'
Rstrip function
• The rstrip() function removes characters from right based on the
argument (a string specifying the set of characters to be removed) and
returns a copy of the string by removing the trailing characters. The
syntax is

string name.rstrip(characters)

characters - a string specifying the set of characters to be removed


from the left of the string. If the chars argument is not
provided, all trailing whitespaces are removed from the
string.
Rstrip function
>>> s=' hello world ' >>> s='$$hello$$$'
>>> s >>> s.rstrip('$')
' hello world ' ‘$$hello‘
>>> s.rstrip() >>> s.rstrip('$l')
‘ hello world' ‘$$hello'
>>> s.rstrip('h')
' hello world '
>>> s='hello world'
>>> s.rstrip(‘d')
‘hello worl'
>>> s.rstrip(‘dl')
‘hello wor'
>>> s.rstrip(‘h')
'hello world'
Join function
• The join() function provides a flexible way to create strings from
iterable objects. It joins each element of an iterable (such as list,
string, dictionary, set and tuple) by a string separator (the string on
which the join() function is called) and returns the concatenated string.
The syntax is

string.join(iterable)

iterable - examples of iterables are list, string, dictionary, tuple and


set. A string must be specified as the separator. If the iterable contains
any non-string values, it raises an error.
Join function
>>> s=['one', 'two', 'three']

>>> '-'.join(s)
'one-two-three‘

>>> s1='Python'
>>> sep='-‘

>>> sep.join(s1)
'P-y-t-h-o-n‘

>>> s3=123 #works only on strings not numbers


>>> sep.join(s3)
Ljust function
• The ljust() function will left align the string i.e. it returns a left-
justified string of a given minimum width. The syntax is

string name.ljust(width, fill character)

width - width of the given string. If width is less than or equal to the
length of the string, the original string is returned.
fillchar (Optional) - character to fill the remaining space of the width.
Ljust function
>>> s='Python Programming'
>>> s.ljust(50,'*')
'Python Programming********************************'

>>> s.ljust(100,'-')
'Python Programming-----------------------------------------------------------
-----------------------'
>>> s.ljust()
Traceback (most recent call last):
File "<pyshell#125>", line 1, in <module>
s.ljust()
TypeError: ljust expected at least 1 argument, got 0

>>> s.ljust(10)
'Python Programming'
Rjust function
• The rjust() function will right align the string i.e. it returns a right-
justified string of a given minimum width. The syntax is

string name.rjust(width, fill character)

width - width of the given string. If width is less than or equal to the
length of the string, the original string is returned.
fillchar (Optional) - character to fill the remaining space of the width.
Rjust function
>>> s='Python Programming‘

>>> s.rjust(50)
' Python Programming‘

>>> s.rjust(50,'*')
'********************************Python Programming‘

>>> s.rjust(10,'*')
'Python Programming'
Center function
• The center() function will center align the string i.e. it returns a center-
justified string of a given minimum width. The syntax is

string name.center(width, fill character)

width - width of the given string. If width is less than or equal to the
length of the string, the original string is returned.
fillchar (Optional) - character to fill the remaining space of the width.
Center function
>>> s='Python Programming‘

>>> s.center(50)
' Python Programming '

>>> s.center(50,'*')
'****************Python Programming****************'

>>> s.center(10,'*')
'Python Programming'

You might also like