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"
[Link](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:
[Link]() [Link](substring)
len(s) [Link](substring,startindex)
startindex specifies the starting position
upper() to search
- Returns a string with all uppercase [Link](substring,startindex,e
letters. ndindex)
-Syntax: startindex specifies the starting position
[Link]() to search
- Example: endindex specifies the last position to
>>>s="hello" search
>>>s1=[Link]() - Example:
>>>print(s1) >>> word = 'banana'
HELLO >>>[Link]('a')
1
lower() >>> [Link]('na')
- Returns a string with all 2
lowercase letters. >>> [Link]('na', 3)
-Syntax: 4
[Link]() >>> [Link]('n', 3, 4)
- Example: -1
>>>s="Hello"
>>>s1=[Link]() index()
>>>print(s1) - Returns Index of Substring
hello - Syntax:
76 [Link](substring)
77
- Example replace()
>>> s="Hello" - Returns a copy of the string where old
>>> print([Link]('e')) substring is replaced with the new
1 substring.
>>>print([Link]("llo")) The original string is unchanged.
2 - Syntax:
[Link](oldstring,newstrin
islower() g)
- Returns True if all alphabets in the - Example:
given string are in Lowercase. Otherwise >>>print([Link]("hello","world"))
returns 'world'
False >>> print(s)
- Syntax: hello
[Link]()
- Example: split()
>>> s="hello" - Splits String from Left
>>>print([Link]()) - 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 [Link]()
False. -Example:
- Syntax: >>>s="Hello World"
[Link]() >>>print([Link]())
- Example: ['Hello', 'World']
>>> s="hello"
>>>print([Link]()) join()
False - Returns a Concatenated String
- Example:
count() >>>s="Hello World"
- Returns the number of occurrences of >>>print([Link]())
the given substring. ['Hello', 'World']
- Syntax: >>>print("-".join(s))
[Link](substring) H-e-l-l-o- -W-o-r-l-d
- Example: casefold()
>>>s="hello" - converts to casefolded strings
>>>[Link]("l") -Syntax:
2 [Link]()
>>>[Link]("e") -Example:
1 >>>s="Hello"
>>>[Link]("w") >>>print([Link]())
0 hello
78 79