Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python strip

by suresh

Python strip string function is one of the Python String methods. This Python strip function removes the specified characters from both the Right-hand side and Left-hand side of a string (by default, White spaces) and returns a new string.

The Python strip function is the combination of Python rstrip and Python lstrip functions. Let us see the syntax of the Python strip string function is

String_Value.strip(Chars)
  • String_Value: A valid string literal.
  • Chars: This parameter is optional. If it omitted, the strip Function considers the white spaces as a default parameter. To change the default value, Please specify the Characters to strip from a string.

Python strip function Example

The following set of examples helps to know the strip Function in Python.

# Strip Method Example

Str1 = '  Tutorial Gateway  ';

Str2 = Str1.strip()
print('Stripping White spaces using Strip() is =', Str2)

# Observe the Original String
print('Converted String is =', Str1.strip())
print('Original String is =', Str1)

# Performing Strip() function directly
Str3 = '00000000Tutorial Gateway00000000'.strip('0')
print("Stripping 0's using Strip() is =", Str3)

Str4 = '+++++Tutorial Gateway****'.strip('+*')
print('Stripping + and * on both sides using Strip() is =', Str4)

# Stripping Left Side
Str5 = '***********Tutorial Gateway'.strip('*')
print('Stripping Left Side using Strip() is =', Str5)

# Stripping Right Side
Str6 = 'Tutorial Gateway========='.strip('=')
print('Stripping Right Side using Strip() is =', Str6)

OUTPUT

Python Strip

ANALYSIS

Within this String function example, First, we declared the String variable Str1 and assigned a string with empty spaces on both sides.

Str1 = '  Tutorial Gateway  ';

It removes the white spaces from both the Left & Right-hand side of String variable Str1 using Strip function and prints the output.

Str2 = Str1.strip()
print('Stripping White spaces using Strip() is =', Str2)

The String strip function returns the output in a new string, instead of altering the original string.

print('Converted String is =', Str1.strip())
print('Original String is =', Str1)

From the above screenshot, see the original string Str1 is unchanged. To change the original String, write the following Python statement

Str1 = Str1.strip()

The Python strip function allows using parameters. Within the following statements, we have zeros on both sides, and we are passing ‘0’ as the argument to strip function, which means it removes zeros from both sides.

Str3 = '00000000Tutorial Gateway00000000'.strip('0')
print("Stripping 0's using Strip() is =", Str3)

Here, we used two characters to strip (+ and *)

Str4 = '+++++Tutorial Gateway****'.strip('+*')
print('Stripping + and * on both sides using Strip() is =', Str4)

It is stripping only the left-hand side of a given string using strip function (Acting like lstrip function).

Str5 = '***********Tutorial Gateway'.strip('*')
print('Stripping Left Side using Strip() is =', Str5)

The strip function was stripping only the Right-hand side of the string (Acting like rstrip function).

Str6 = 'Tutorial Gateway========='.strip('=')
print('Stripping Right Side using Strip() is =', Str6)

Please refer to Python rstrip and Python lstrip function articles.

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy