Python rindex() method
works same as the rfind() method except it throws error ValueError. This method
throws an exception ValueError if the substring is not found. The syntax is
given below.
Syntax
rindex(sub[, start[, end]])
Parameters
sub : substring to be searched.
start (optional) : starting index to
start searching.
end (optional) : end index where to
search stopped.
Return
It returns either index of substring or
throws an exception ValueError.
Python String rindex() Method Example
A simple example is created first to
see how to implement this method. This method returns index of the substring.
# Python rindex() method example
# Variable declaration
str = “It is technical tutorial”
# calling function
str2 = str.rindex(“t”) # No start and
end is given
# displaying result
print(str2)
Output:
18
Python String rindex() Method Example
This method accept parameters start and
end index to search subtring from the substring. See the example below.
# Python rindex() method example
# Variable declaration
str = “It is technical tutorial”
# calling function
str2 = str.rindex(“t”) # No start and
end is given
# displaying result
print(str2)
str2 = str.rfind(“t”,5,15) # Start is
end both are given
print(str2)
Output:
18
6
Python String rindex() Method Example
If the substring is not found in the
string, it raises ValueError. See the example below.
# Python rindex() method example
# Variable declaration
str = “Hello C Language”
# calling function
str2 = str.rindex(“t”) # No start and
end is given
# displaying result
print(str2)
str2 = str.rfind(“t”,5,15) # Start is
end both are given
print(str2)
Output:
ValueError: substring not found
Python rjust() method right
justify the string and fill the remaining spaces with fillchars. This method
returns a new string justified right and filled with fillchars.
Syntax
rjust(width[, fillchar])
Parameters
width : width of the given string.
fillchar : characters to fill the
remaining space in the string. It is optional.
Return
It returns a right justified string.
Python String rjust() Method Example
# Python rjust() method example
# Variable declaration
str = 'Javatpoint'
# Calling function
str = str.rjust(15,”$”)
# Displaying result
print(str)
Output:
$$$$$javapoint
Python rstrip() method
removes all the trailing characters from the string. It means it removes all
the specified characters from right side of the string. If we don't specify the
parameter, It removes all the whitespaces from the string. This method returns
a string value.
Syntax
rstrip([chars])
Parameters
chars: character to be removed from the
string.
Return
It returns string.
Python String rstrip() Method Example
A simple example, does not take any
parameter. It removes all the trailing whitespaces from the string.
# Python rstrip() method example
# Variable declaration
str = “Java and C# “
# Calling function
str2 = str.rstrip()
# Displaying result
print(“Old string: “,str)
print(“New String: “,str2)
Output:
Old string: Java and C#
New String: Java and C#
Python rsplit() method
seperates the string and returns a list. It splits from the right using
seperator as a delimiter. If seperator is not specified, any whitespace string
is a separator. This method does same as split() except splitting from the
right which is described in detail below.
Note: if separator is not given,
whitespace is treated as separator.
Syntax
rsplit(sep=None,maxsplit=-1)
Parameters
sep: A string parameter acts as a
seperator.
maxsplit: number of times split
perfomed.
Return
It returns a comma separated list.
Python String rsplit() Method Example
This is a simple example to understand
the usage of rsplit() method.
# Python rsplit() method example
# Variable declaration
str = “Java is a programming
language”
# Calling function
str2 = str.rsplit()
# Displaying result
print(str2)
Output:
['Java', 'is', 'a', 'programming',
'language']
Python String rsplit() Method Example
Let's pass a parameter separator to the
method, see the example.
# Python rsplit() method example
# Variable declaration
str = “Java is a programming
language”
# Calling function
str2 = str.rsplit('Java')
# Displaying result
print(str2)
Output:
['', ' is a programming language']
Python split() method
splits the string into a comma separated list. It separates string based on the
separator delimiter. This method takes two parameters and both are optional. It
is described below.
Syntax
split(sep=None, maxsplit=-1)
Parameters
sep: A string parameter acts as a
seperator.
maxsplit: Number of times split
perfome.
Return
It returns a comma separated list.
Python String split() Method Example
This is a simple example to understand
the usage of split() method. No parameter is given, by default whitespaces work
as separator. See the example below.
# Python split() method example
# Variable declaration
str = “Java is a programming
language”
# Calling function
str2 = str.split()
# Displaying result
print(str)
print(str2)
Output:
Java is a programming language
['Java', 'is', 'a', 'programming',
'language']
Python String split() Method Example
Let's pass a parameter separator to the
method, now it will separate the string based on the separator. See the example
below.
# Python split() method example
# Variable declaration
str = “Java is a programming
language”
# Calling function
str2 = str.split('Java')
# Displaying result
print(str2)3
Output:
['', ' is a programming language']
Python splitlines() method
splits the string based on the lines. It breaks the string at line boundaries
and returns a list of splitted strings. Line breakers can be a new line (\n),
carriage return (\r) etc. A table of line breakers are given below which split
the string.
This method splits on the given line
boundaries.
Representation
|
Description
|
\n
|
Line Feed
|
\r
|
Carriage Return
|
\r\n
|
Carriage Return + Line Feed
|
\v or
\x0b
|
Line Tabulation
|
\f or \x0c
|
Form Feed
|
\x1c
|
File Separator
|
\x1d
|
Group Separator
|
\x1e
|
Record Separator
|
\x85
|
Next Line (C1 Control Code)
|
\u2028
|
Line Separator
|
\u2029
|
Paragraph Separator
|
Syntax
splitlines([keepends])
Parameters
keepends: It is a boolean value which
can be either True or False. It is optional.
Return
It returns a comma separated list of
lines.
Python String splitlines() Method
Example
# Python splitlines() method
example
# Variable declaration
str = “Java is a programming
language”
# Calling function
str2 = str.splitlines() # returns a
list having single element
print(str)
print(str2)
str = “Java \n is a programming \r
language”
str2 = str.splitlines() # returns a
list having splitted elements
print(str2)
Output:
Java is a programming language
['Java is a programming language']
['Java ', ' is a programming ', '
language']
Python String splitlines() Method
Example
Passing True to the method which causes
to include line breakers into the list of string. See the example below.
# Python splitlines() method
example
# Variable declaration
str = “Java \n is a programming \r
language”
# Calling function
str2 = str.splitlines(True) # returns a
list having splitted elements
print(str2)
Output:
['Java \n', ' is a programming \r', '
language']
Python startswith() method
returns either True or False. It returns True if the string starts with the
prefix, otherwise False. It takes two parameters start and end. Start is a
starting index from where searching starts and end index is where searching
stops.
Syntax
startswith(prefix[, start[,
end]])
Parameters
prefix : A string which is to be
checked.
start : Start index from where
searching starts.
end : End index till there searching
performs.
Both start and end are optional
parameters.
Return
It returns boolean value either True or
False.
Python String startswith() Method
Example
Let's first create a simple example
which prints True if the string starts with the prefix.
# Python String startswith()
method
# Declaring variable
str = “Hello Javatpoint”
# Calling function
str2 = str.startswith(“Hello”)
# Displaying result
print (str2)
Output:
True
Python String startswith() Method
Example
If the string does not start with
prefix, the method returns False. See the example below
# Python String startswith()
method
# Declaring variable
str = “Hello Javatpoint”
# Calling function
str2 = str.startswith(“Java”) #
False
# Displaying result
print (str2)
Output:
False
Python swapcase() method
converts case of the string characters from uppercase to lowercase and vice
versa. It does not require any parameter and returns a string after case
conversion.
Syntax
swapcase()
Parameters
No Parameter
Return
It returns a string.
Python String swapcase() Method Example
It is a simple example to convert
uppercase to lowercase using the swapcase() method.
# Python String swapcase() method
# Declaring variable
str = “HELLO JAVATPOINT”
# Calling function
str2 = str.swapcase()
# Displaying result
print (str2)
Output:
hello javatpoint
Python String swapcase() Method Example
This example describes conversion from
lowercase to uppercase.
# Python String swapcase() method
# Declaring variable
str = “hello javatpoint”
# Calling function
str2 = str.swapcase()
# Displaying result
print (str2)
Output:
HELLO JAVATPOINT
Python translate() method a
string in which each character has been mapped through the given translation
table. We can use maketrans() method to create a translation map from
character-to-character mappings in different formats.
Syntax
translate(table)
Parameters
table : A translation table.
Return
It returns a string.
Python String translate() Method
Example
# Python translate() method
# Declaring table and variables
table = { 97 : 103, 111 : 112, 117 :
None }
str = “Hello javatpoint”
# Calling function
str2 = str.translate(table)
# Displaying result
print(str2)
Output:
Hellp jgvgtppint
Python upper() method
converts all the character to uppercase and returns a uppercase string.
Syntax
upper()
Parameters
No parameters
Return
It returns a string.
Python String upper() Method Example
First see a simple example of upper()
method. This method returns a uppercase string.
# Python upper() method
# Declaring table and variables
str = “Hello javatpoint”
# Calling function
str2 = str.upper()
# Displaying result
print(str2)
Output:
HELLO JAVATPOINT
Python zfill() method fills
the string at left with 0 digit to make a string of length width. It returns a
string contains a sign prefix + or - before the 0 digit.
It returns original length string if
the width is less than string length.
Syntax
zfill(width)
Parameters
width : length of the string.
Return
It returns a string.
Python String zfill() Method Example
Let's see an example which has width
greater than string length. It returns a new string containing 0 to the left
and total length is equal to width.
# Python zfill(width)
method
# Declaring variables
text = “Zfill Example”
# Calling Function
str2 = text.zfill(20)
# Displaying result
print(str2)
Output:
0000000Zfill Example
Python String zfill() Method Example
This example describes the sign either
+ or - includes before the zero fills to the left of the string. See every value
is filled after prefixing (+ or -) sign.
# Python zfill(width)
method
# Declaring variables
val = “+100”
val2 = “-200”
val3 = “--Rohan--”
# Calling Function
str2 = val.zfill(10)
str3 = val2.zfill(10)
str4 = val3.zfill(10)
# Displaying result
print(str2)
print(str3)
print(str4)
Output:
+000000100
-000000200
-0-Rohan—
Python Title() method is
the Python String Method which is used to convert the first character in each
word to Uppercase and remaining characters to Lowercase in string and returns
new string.
Syntax:
str.Title()
parameters:str is a valid string which
we need to convert.
return: This function returns a string
which has first letter in each word is uppercase and all remaining letters are
lowercase.
# Python Title() Method Example
str1 = 'geeKs foR geEks'
str2 = str1.title()
print ('First Output after Title()
method is = ', str2 )
# observe the original string
print ('Converted String is = ',
str1.title() )
print ('Original String is = ', str1 )
# Performing title() function directly
str3 = 'ASIPU pawan kuMAr'.title()
print ('Second Output after Title()
method is = ', str3 )
str4 = 'stutya kUMari sHAW'.title()
print ('Third Output after Title()
method is = ', str4 )
str5 = '6041'.title()
print ('Fourth Output after Title()
method is = ', str5 )
Output:
First Output after Title() method is
= Geeks For Geeks
Converted String is = Geeks
For Geeks
Original String is = geeKs
foR geEks
Second Output after Title() method is
= Asipu Pawan Kumar
Third Output after Title() method is = Stutya
Kumari Shaw
Fourth Output after Title() method is
= 6041
Python rpartition() method
splits the string at the last occurrence of seperator substring.It splits the
string from the last occurrence of parameter and returns a tuple. The tuple
contains the three parts before the separator, the separator itself, and the
part after the separator.
It returns an empty tuple having
seperator only, if the seperator not found.
The method Syntax is given below.
Syntax
rpartition(sep)
Parameters
sep: A string parameter which separates
the string.
Return
It returns a tuple, A 3-Tuple.
Python String rpartition() Method
Example
Let's see a simple use of partition
method in various scenario.
# Python rpartition() method
example
# Variable declaration
str = “Java is a programming
language”
# Calling function
str2 = str.rpartition(“is”)
# Displaying result
print(str2)
# seperator is at begining
str2 = str.rpartition(“Java”)
print(str2)
# seperator at ent
str2 = str.rpartition(“language”)
print(str2)
# when seperater is a substring
str2 = str.rpartition(“av”)
print(str2)
Output:
('Java ', 'is', ' a programming
language'')
('', 'Java', ' is a programming
language')
('Java is a programming ', 'language',
'')
('J', 'av', 'a is a programming
language')
Python strip() method
returns a copy of the string with both leading and trailing characters removed
(based on the string argument passed).
The strip() removes characters from
both left and right based on the argument (a string specifying the set of
characters to be removed).
The syntax of strip() is:
string.strip([chars])
Parameters
chars (optional) - a string specifying
the set of characters to be removed.
If the chars argument is not provided,
all leading and trailing whitespaces are removed from the string.
Return Value from strip()
The strip() returns a copy of the
string with both leading and trailing characters stripped.
When the combination of characters in
the chars argument mismatches the character of the string in the left, it stops
removing the leading characters.
Similarly, when the combination of
characters in the chars argument mismatches the character of the string in the
right, it stops removing the trailing characters.
Example:
string = ' xoxo love
xoxo '
# Leading whitepsace are removed
print(string.strip())
print(string.strip(' xoxoe'))
# Argument doesn't contain space
# No characters are removed.
print(string.strip('sti'))
string = 'android is awesome'
print(string.strip('an'))
Output:
xoxo love xoxo
lov
xoxo love xoxo
droid is awesome
No comments:
Post a Comment