Min menu

Pages

Processing functions in the str class to handle text in Python

Python  functionexpandtabs()

its definition

Returns a copy of the text that called it with double the size of the blank spaces(Tab Spaces)that were added in the text by the symbol \t.
By default, the symbol \trepresents 4 blank spaces when displaying text, but in the version returned by the function expandtabs(), each symbol will be converted \tto 8 blank spaces unless you specify the number of spaces yourself.



built

                  str.expandtabs(tabsize=8)
  


parameters

tabsizeAn integer representing an integer whose type intrepresents the number of blank spaces that will be placed in place of each symbol placed \tin the text.



Return value

Returns a copy of the text that called it with double the size of the blank spaces(Tab Spaces)that were added in the text by the symbol \t.


Example

Test.py
                    # at the beginning of the text, ie it contains 4 blank spaces \t contains the symbol s here we have defined a text variable named
	  s = '\tWe add a tab space at the beginning.'

	  # s Here we have shown the text in the variable
	  print(s)

	  # With 8 blank spaces \t after the s has been swapped here we display the text in the variable
	  print(s.expandtabs())

	  # With 12 blank spaces \t after the s has been swapped here we display the text in the variable
	  print(s.expandtabs(12))
	

We will get the following result when running.

    We add a tab space at the beginning.
        We add a tab space at the beginning.
            We add a tab space at the beginning.

Python  functionlstrip()

its definition

Returns a copy of the invoked text with any blank space at the beginning removed.



built

                        str.lstrip()
  


parameters

Do not accept any parameters.



Return value

Returns a copy of the invoked text with any blank space at the beginning removed.



Example

Test.py
                          s = ' Python tutorial ' # ​​We put some blank spaces at the beginning s Here we have defined a text variable named

	  print(s) # as s Here we have printed the text in the variable
	  print(s.lstrip()) #s when called on the variable lstrip() here we have printed what the function will return
	

We will get the following result when running.

     Python tutorial
Python tutorial

Python  functionrstrip()

its definition

Returns a copy of the invoked text with any blank space at the end removed.



built

                  str.rstrip()
  


parameters

Do not accept any parameters.



Return value

Returns a copy of the invoked text with any blank space at the end removed.



Example

Test.py
                    s = 'Python tutorial ' # ​​We put some blank spaces at the end of it s Here we have defined a text variable named

	  print(s) # as s Here we have printed the text in the variable
	  print(s.rstrip()) #s when called on the variable rstrip() here we have printed what the function will return
	

We will get the following result when running.
We have marked the blank spaces at the end of the first line in yellow so you can notice the difference.

Python tutorial Python tutorial      

Python  functionstrip()

its definition

Returns a copy of the invoked text, omitting any blank space at the beginning and end of it.
By default, this function deletes the empty spaces in the first or the end of the text, but if you want to delete certain characters(instead of blank spaces)If they are at the beginning and end of the text, you can pass these characters in place of the parameter chars.



built

                  str.strip([chars])
  


parameters

Parameter location charsYou can pass the text that you want to delete from the version that the function will return if it is at the beginning or end of the text that called it.



Return value

Returns a copy of the invoked text, omitting any blank space at the beginning and end of it.
If you pass a text in place of the parameter charsit erases it if it is at the beginning and end of the text that called it instead of clearing the blank spaces.



first example

Test.py
                    s = ' Python tutorial ' # ​​We put at the beginning and end of some blank spaces s here we have defined a text variable named

	  print(s) # as s Here we have printed the text in the variable
	  print(s.strip('')) # s when called on the variable strip() here we have printed what the function will return
	

We will get the following result when running.
We have marked the blank spaces in the first line in yellow so you can notice the difference.

    Python tutorial Python tutorial     


second example

Test.py
                    s = '--- Python tutorial ----' # '- ' We put some symbols at the beginning and end of s here we have defined a text variable named

	  print(s) # as s Here we have printed the text in the variable
	  print(s.strip('-')) # s when called on the variable strip() here we have printed what the function will return
	

We will get the following result when running.
We have marked the blank spaces in the first and second lines in yellow. Note that it has not been deleted, only symbols have been deleted -.

----  Python tutorial  ----
 Python tutorial 

Python  functionljust()

its definition

Returns a copy of the invoked text with blank spaces at the end if the number of characters exceeds the number we pass in place of the parameter width.
And you can pass any character or symbol in place of the parameter fillcharto appear instead of blank spaces.



built

                  str.ljust(width[, fillchar])
  


parameters

  • The location of the parameter widthwe pass a number that specifies the length of the line, then if the text length is less than the length of the line, empty spaces will be added after it.

  • fillcharIt is an optional parameter that you can pass in place of any character until you want to put it instead of putting blank spaces.



Return value

Returns a copy of the text that called it and may add some blank spaces or some characters at the end of it depending on the number of characters in it.



Example

Test.py
                    s = 'Python tutorial' # s Here we have defined a text variable named

	  print(s) # as s Here we have printed the text in the variable
	  print(s.ljust(30)) # until the number of characters is 30 s after adding some blank spaces at the end of the text of the variable lstrip() here we print what the function will return
	  print(s.ljust(30, '-')) # until the number of characters is 30 s after adding the '-' symbol several times in the last body of the lstrip() variable here we print what the function will return
	

We will get the following result when running.
We have marked what has been added on the second and third lines in yellow so that you can notice the difference.

Python tutorial
Python tutorial Python tutorial ---------------               

Python  functionrjust()

its definition

Returns a copy of the invoked text with blank spaces in its beginning if the number of characters exceeds the number we pass in place of the parameter width.
And you can pass any character or symbol in place of the parameter fillcharto appear instead of blank spaces.



built

                  str.rjust(width[, fillchar])
  


parameters

  • The location of the parameter widthwe pass a number that specifies the length of the line, then if the text length is less than the length of the line, empty spaces will be added before it.

  • fillcharIt is an optional parameter that you can pass in place of any character until you want to put it instead of putting blank spaces.



Return value

Returns a copy of the text that called it and may add some blank spaces or some characters in its beginning depending on the number of characters in it.



Example

Test.py
                    s = 'Python tutorial' # s Here we have defined a text variable named

	  print(s) # as s Here we have printed the text in the variable
	  print(s.rjust(30)) # until the number of characters is 30 s after adding some blank spaces in the first text of the lstrip() variable here we print what the function will return
	  print(s.rjust(30, '-')) # until the number of characters is 30 s After adding the '-' symbol several times in the first body of the lstrip() variable here we print what the function will return
	

We will get the following result when running.
We have marked what has been added on the second and third lines in yellow so that you can notice the difference.

Python tutorial
               Python tutorial
--------------- Python tutorial