What does input () strip () do in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    strip() is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed). 
    Syntax: 

    string.strip([chars])
    Parameter: 
    There is only one optional parameter in it:
    1)chars - a string specifying 
    the set of characters to be removed. 
    
    If the optional chars parameter is not given, all leading 
    and trailing whitespaces are removed from the string.
    Return Value:
    Returns a copy of the string with both leading and trailing characters removed.

    Python3

    string =

    print(string)

    print(string.strip())  

    print(string.strip(' geeks'))

    Output:

        geeks for geeks     
    geeks for geeks
    for

    Python3

    str1 = 'geeks for geeks'

    print(str1)

    str2 = 'ekgs'

    print(str1.strip(str2))

    Output:

    geeks for geeks
     for

    Working of above code : 
    We first construct a string str1 = ‘geeks for geeks’ 
    Now we call strip method over str1 and pass str2 = ‘ekgs’ as argument. 
    Now python interpreter trace str1 from left.It remove the character of str1 if it is present in str2. 
    Otherwise it stops tracing. 
    Now python interpreter trace str1 from right. It remove the character of str1 if it is present in str2. 
    Otherwise it stop tracing. 
    Now at last it returns the resultant string.
    When we call strip() without argument, it removes leading and trailing spaces. 
     

    Python3

    str1 =

    print(str1)

    print(str1.strip())

    Output: 

    geeks for geeks

    Practical application: 
    Given a string remove occurrence of word “the” from the beginning and the end. 
     

    Python

    string = " the King has the largest army in the entire world the"

    print(string.strip(" the"))

    Input:

    the King has the largest army in the entire world the

    Output: 

    King has the largest army in the entire world

    If I want to take a number as input, would I also need the .strip() method? Like this:

    n = int(input().strip())
    

    Instead of just coding:

    n = int(input())
    

    I know .strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string. But I wonder why / if it is necessary.

    MSeifert

    137k32 gold badges317 silver badges331 bronze badges

    asked Jan 13, 2018 at 18:16

    2

    It isn't necessary when you cast it to an integer with int because int already handles (ignores) leading and trailing whitespaces*:

    >>> int('1 ')
    1
    >>> int(' 1')
    1
    >>> int(' 1\n\t')  # also handles other spaces like newlines or tabs
    1
    

    It's mostly important to strip the whitespaces if you use sys.stdin.readline (which contains a trailing newline character) and you don't know if the function that uses that value can handle additional whitespaces.


    * Just FYI: The types float, complex, fractions.Fraction, and decimal.Decimal also ignore leading and trailing whitespaces so you don't need to strip the strings if you use any of those.

    answered Jan 13, 2018 at 18:24

    MSeifertMSeifert

    137k32 gold badges317 silver badges331 bronze badges

    3

    Now you can just use new library named ninput and use function named int_input() for example

    import ninput
    n = ninput.int_input()
    

    https://pypi.org/project/ninput/0.1.2/ there is library docs and files

    answered Dec 20, 2021 at 20:39

    1

    What is input () Strip ()?

    strip() is an inbuilt function in Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed). Syntax: string.

    What does Strip () do in Python?

    The Strip() method in Python removes or truncates the given characters from the beginning and the end of the original string. The default behavior of the strip() method is to remove the whitespace from the beginning and at the end of the string.

    What does Strip and split do in Python?

    There's no difference. split() ignores whitespace on the ends of the input by default. People call strip() first either because they think it's clearer or because they don't know this behavior of split() .