Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Python String methods - rjust(), ljust(), rfind(), replace(), len(), lower(), strip(), upper() and more

1 min 6 sec read

Python strings can be written within single quotes '' or double quotes "". To print the length of string you can use len() function of string. Python does not support char datatype.

Python 3 String methods

Let's discuss each of Python 3 string methods in detail with syntax and example.

Methods Description
rjust() Returns a right justified version of the given string. It takes length and charcater as a prameter. By default charcater is " "(space).
ljust() Returns a left justified version of the given string. It takes length and charcater as a prameter. By default charcater is " "(space).
center() Returns a center version of the given string. It takes length and charcater as a prameter. By default charcater is " "(space).
upper() Returns a uppercase version of the given string. Converts a string into upper case.
lower Returns a lowercase version of the given string. Converts a string into lower case.
capitalize() Converts the first charcater as capital letter and keep the rest string as same.
replace() Replace all instances of one substring with another.
strip Strip leading and trailing whitespace
len() Returns the length of the given string.
rfind() Searches for the text in the string and return the last occurrence of that string. It takes value, start, end as parameter.

Python string ljust() Method

The python string ljust() method returns the left justfied version of the given string. Basic syntax of ljust() method is given below,

Syntax:

ljust(length, character) 

Here, length parameter is required and used to return the length of string. charcter parameter is optional. This is used to fill the missing space (to the left of the string). Default is " " (space).

Example

str = "Hello"
x = str.ljust(10, "*")
print(x)

Output

Hello*****

Python string rjust() Method

The python string rjust() method returns the right justfied version of the given string. Basic syntax of rjust() method is given below,

Syntax:

rjust(length, character) 

Here, length parameter is required and used to return the length of string. charcter parameter is optional. This is used to fill the missing space (to the left of the string). Default is " " (space).

Example

str = "Hello"
x = str.rjust(10, "*")
print(x)

Output

*****Hello

Python string center() Method

The python string center() method returns the center version of the given string. Basic syntax of center() method is given below,

Syntax:

center(length, character) 

Here, length parameter is required and used to return the length of string. charcter parameter is optional. This is used to fill the missing space (to the left of the string). Default is " " (space).

Example

str = "Hello"
x = str.center(10, "*")
print(x)

Output

**Hello***

Python string rfind() Method

The python string rfind() method searches the text in the string and returns the last occurence of that string. If the value is not present in the string, it returns -1 else 1. Basic syntax of rfind() method is given below,

Syntax:

rfind(value, start, end) 

Here, value parameter is required. The value to search for in the given string.. start parameter is optional. Position to start the search from. end parameter is optional. Position to end the search.

Example

str = "Hello"
x = str.rfind('e')
print(x)

Output

1

Example

str = "Hello"
x = str.rfind('e',1,4)
print(x)

Output

1

Example

str = "Hello"
x = str.rfind('e',2,4)
print(x)

Output

-1

This example illustrates various python string methods. Each line in code has comments to illustrate methods use.

# You can use single quotes for initialising string literals string1 = "hello" # or can use double quotes also string2 = "world" # String length print(len(string1)) # String Concatenation using + operator print(string1 + " " + string2) # sprintf style string formatting print("%s %s %d" % (string1, string2, 30))
s = "codzify" # Capitalize a string print(s.capitalize()) # Convert a string to uppercase print(s.upper()) # Convert a string to lowercase print(s.lower()) # Right-justify a string, padding with spaces print(s.rjust(10)) # Center a string, padding with spaces print(s.center(10)) # Replace all instances of one substring with another print(s.replace("fy", "ffyy")) # Strip leading and trailing whitespace print(" .com ".strip()) # Returns the position of the last occurance of given string print("codzify".rfind("o"))

Recommended Articles

  1. Python Numbers
  2. Python Strings
  3. Python Loops
  4. Python List Comprehension
  5. Python Sets
  6. Python Tuple

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor