What Is Python Whitespace and How Does It Work

Does whitespace in syntax affect performance in Python?

This was an interesting question. Basically the use of white spaces between the operands and the operator is to increase readability. It's just a matter of personal choice to add one white space or add ten. The interpreter/compiler doesn't care about white spaces. It's just about readability.

Now when you do something like this-

a = 10
b = 100
c = a + b

And when you do something like this-

a       =          10
b = 100
c = a + b

You'll notice that the first one is more readable than the second one, but this doesn't mean that the second one is wrong. It is just a matter of personal choice. Basically the convention says to follow the first method but we would get the same output with or without white spaces.

So you may use one or ten white spaces in your program, nobody can question you!!

How do add whitespace in between a word in python

If you want to add more whitespaces, you can just added it inside the string. If you want a new line, you can use "\n" in your string, which indicate the start of a new line.

Check this link to find out more about escape characters:

https://www.tutorialspoint.com/escape-characters-in-python

Are there any pitfalls with using whitespace in Python?

yeah there are some pitfalls, but most of the time, in practice, they turn out to be enemy windmills of the Quixotic style, i.e. imaginary, and nothing to worry about in reality.

I would estimate that the pitfalls one is most likely to encounter are (including mitigating steps identified):

  1. working with others a.k.a. collaboration

    a. if you have others which for whatever reason refuse to adhere to PEP 8, then it could become a pain to maintain code. I've never seen this in practice once I point out to them the almost universal convention for python is indent level == four spaces

    b. get anyone/everyone you work with to accept the convention and have them figure out how to have their editor automatically do it (or better yet, if you use the same editor, show them how to configure it) such that copy-and-paste and stuff just works.

  2. having to invest in a "decent" editor other than your current preferred one, if your current preferred editor is not python friendly -- not really a pitfall, more an investment requirement to avoid the other pitfalls mentioned associated with copy-and-paste, re-factoring, etc. stop using Notepad and you'll thank yourself in the morning.

    a. your efficiency in editing the code will be much higher under an editor which understands python

    b. most modern code editors handle python decently. I myself prefer GNU Emacs, and recent versions come with excellent python-mode support out-of-the-box. The are plenty of other editors to explore, including many free alternatives and IDEs.

    c. python itself comes out of the box with a "smart" python editor, idle. Check it out if you are not familiar, as it is probably already available with your python install, and may even support python better than your current editor. PyCrust is another option for a python editor implemented in python, and comes as part of wxPython.

  3. some code generation or templating environments that incorporate python (think HTML generation or python CGI/WSGI apps) can have quirks

    a. most of them, if they touch python, have taken steps to minimize the nature of python as an issue, but it still pops up once in a while.

    b. if you encounter this, familiarize yourself with the steps that the framework authors have already taken to minimize the impact, and read their suggestions (and yes they will have some if it has ever been encountered in their project), and it will be simple to avoid the pitfalls related to python on this.

How do I trim whitespace?

For whitespace on both sides, use str.strip:

s = "  \t a string example\t  "
s = s.strip()

For whitespace on the right side, use str.rstrip:

s = s.rstrip()

For whitespace on the left side, use str.lstrip:

s = s.lstrip()

You can provide an argument to strip arbitrary characters to any of these functions, like this:

s = s.strip(' \t\n\r')

This will strip any space, \t, \n, or \r characters from both sides of the string.

The examples above only remove strings from the left-hand and right-hand sides of strings. If you want to also remove characters from the middle of a string, try re.sub:

import re
print(re.sub('[\s+]', '', s))

That should print out:

astringexample

Whitespace problems with python

When I copy your code to a file, test.py, and run

cat -A test.py

I see

//How it appears in my editor$
def SaveList(self, directory):$
templist = []$
templistbox2 = []$
for n,i in enumerate(self.listbox2.get(0,END)): $
templistbox2.insert(n, re.sub(r'^[0-9]*[.]',"",str(i)))$
for filename in sorted(os.listdir(directory)):$
self.templist.insert(i, filename)$
^I print filename #whitespace error here$

which indicates there is a tab (represented by ^I) followed by four spaces on the last line.

I'm not sure what the equivalent tool on Windows would be, but the Mac should have the cat -A command. It will show you where the tabs versus spaces are.


There is a program called reindent.py which will convert tabs to spaces for you:

reindent.py test.py

On Unix there is also a unexpand command which converts spaces to tabs.


Most Python programmers use spaces rather than tabs for indentation. Most of the Python code you find on the web will use spaces rather than tabs.

Your editor may be adding tabs, but if you took a snippet of code from the web, your file may now contain both tabs and spaces.

It is easiest to go with the flow and adopt the spaces-as-indentation convention, so you will not have to reformat other people's code so much.

By the way, adopting the spaces-as-indentation convention does not mean having to press SPACE 4 times for each indentation level. Your editor should have a configuration option which makes pressing TAB insert 4 spaces.

Is there something that looks like white space but it is not and how to remove it?

I have just tired to use ord(c) and I get 0 for the characters that I have interpreted as white spaces.

It indicates that the input data is utf-16 text. If zero bytes follow what appears to be ascii characters e.g., b'a\0' then it is 'utf-16le' (little-endian):

>>> b'd\0a\0t\0a\0'.decode('utf-16le')
u'data'

Don't use .replace(b'\0', b''); it will break on the first non-ascii character e.g., b'\xac ' (euro sign encoded using utf-16le character encoding).

Why does Python add a whitespace after printing int then string?

you can optionally give a separator argument of whatever you want for "how comma should behave" (as another option to the other answers...

print("a","b",sep="+")
a+b

so you could just use ""

print("a","b",sep="")
ab

if you do decide to use a single string as the other answers suggest you should really just use string formatting instead of + concatenation

print("{num}th".format(num=x))

python - removing whitespaces not working - encoding issue?

You can do

data["Prix de vente"] = data["Prix de vente"].str.replace("\s","", regex=True)

Matching "\s" combined with regex=True instead of " " makes sure to match not only regular spaces but any whitespace character

More background:

Your first approach

data["Prix de vente"] = data["Prix de vente"].str.strip()

does not work, since .strip() only removes leading and trailing characters.

Your second approach

data["Prix de vente"] = data["Prix de vente"].replace(" ","")

does not work, since it is using not str.replace, but pd.Series.replace, which only replaces exact matches of values (e.g. "900 000").



Related Topics



Leave a reply



Submit