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

String Methods

Uploaded by

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

String Methods

Uploaded by

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

String Functions and Methods

String methods len()


Strings provide methods that perform a - Returns the length of the given string.
variety of useful operations. -Syntax:
len(stringname)
Syntax for using string functions - Example:
>>>s="Hello"
stringname.functionname(parameters) >>>print(len(s))
5
• This form of dot notation specifies
the name of the method and the find()
name of the string to which it is - Returns the index of given substring.
applied. - If the given substring is not found, then
• The empty parentheses indicate it returns -1.
that this method takes no - By default, find() starts at the beginning
arguments. of the string.
• A method call - It can take upto 3 arguments. Last 2
is called an invocation. arguments are optional.
Example: - Syntax:
s.lower() stringname.find(substring)
len(s) stringname.find(substring,startindex)
startindex specifies the starting position
upper() to search
- Returns a string with all uppercase stringname.find(substring,startindex,e
letters. ndindex)
-Syntax: startindex specifies the starting position
stringname.upper() to search
- Example: endindex specifies the last position to
>>>s="hello" search
>>>s1=s.upper() - Example:
>>>print(s1) >>> word = 'banana'
HELLO >>>word.find('a')
1
lower() >>> word.find('na')
- Returns a string with all 2
lowercase letters. >>> word.find('na', 3)
-Syntax: 4
stringname.lower() >>> word.find('n', 3, 4)
- Example: -1
>>>s="Hello"
>>>s1=s.lower() index()
>>>print(s1) - Returns Index of Substring
hello - Syntax:
76 stringname.index(substring)
77
- Example replace()
>>> s="Hello" - Returns a copy of the string where old
>>> print(s.index('e')) substring is replaced with the new
1 substring.
>>>print(s.index("llo")) The original string is unchanged.
2 - Syntax:
stringname.replace(oldstring,newstrin
islower() g)
- Returns True if all alphabets in the - Example:
given string are in Lowercase. Otherwise >>>print(s.replace("hello","world"))
returns 'world'
False >>> print(s)
- Syntax: hello
stringname.islower()
- Example: split()
>>> s="hello" - Splits String from Left
>>>print(s.islower()) - By default, it uses whitespaces for
True splitting the given string. If no
whitespace is
isupper() there, then same string will be returned
- Returns True if all alphabets in the as list
given string are in Uppercase. Otherwise -Syntax:
returns stringname.split()
False. -Example:
- Syntax: >>>s="Hello World"
stringname.isupper() >>>print(s.split())
- Example: ['Hello', 'World']
>>> s="hello"
>>>print(s.isupper()) join()
False - Returns a Concatenated String
- Example:
count() >>>s="Hello World"
- Returns the number of occurrences of >>>print(s.split())
the given substring. ['Hello', 'World']
- Syntax: >>>print("-".join(s))
stringname.count(substring) H-e-l-l-o- -W-o-r-l-d
- Example: casefold()
>>>s="hello" - converts to casefolded strings
>>>s.count("l") -Syntax:
2 stringname.casefold()
>>>s.count("e") -Example:
1 >>>s="Hello"
>>>s.count("w") >>>print(s.casefold())
0 hello
78 79

You might also like