>> print(s)The do barksThis behaves as expected. The g character disappears because of the \b backspace.Now I try this:>>> s = "The dog bark" name="description" /> >> print(s)The do barksThis behaves as expected. The g character disappears because of the \b backspace.Now I try this:>>> s = "The dog bark" />
84191

Python strings: backspace at the end of string behaves differently

<h3>Question</h3>

I'm experimenting with the backspace character \b in Python strings. I tried this:

>>> s = "The dog\b barks" >>> print(s) The do barks

This behaves as expected. The g character disappears because of the \b backspace.

Now I try this:

>>> s = "The dog barks\b" >>> print(s) The dog barks

Strange... the s character didn't disappear. Why?

Note:
I work on a Windows 10 PC and did these experiments in the Windows cmd prompt (the terminal). I run Python 3.6


<h3>Answer1:</h3>

That isn't a behavior of python but your output StreamHandler. The \b doesn't cancel the character before it, it instructs your output handler device (terminal/console) to move the cursor backward.

What basically happens is, that you are placing the cursor one character to the left and continue streaming what is following. It visually look like you are putting a space as the next following character is aspace.

In Both Unix based and Windows the output behavior is the same:

Unix test / Mac Terminal:

echo -e "Hello test\b" >Hello test

Windows test / CMD / Powershell :

echo "Hello test`b" >Hello test
<h3>Answer2:</h3>

Python doesn't treat the backspace (\b) character specially, but Windows command prompt (cmd.exe) treats the backspace by moving the cursor left one character, then printing the rest of the string. Because there is nothing left to print, moving the cursor left doesn't actually do anything.

来源:https://stackoverflow.com/questions/47732236/python-strings-backspace-at-the-end-of-string-behaves-differently

Recommend

  • HLSL Integer Texture Coordinates
  • Terraform: How to get a boolean from interpolation?
  • Can I use the “as” mechanism in an if statement
  • why does my android spinner display the radio button in the textview?
  • java.lang.NoClassDefFoundError: when trying to run jar
  • ontimeupdate with YouTube API
  • How to increase font size of some part of string in android?
  • SQL Server - simple way to create pivot
  • Converting Excel column letters to corresponding column numbers
  • Crystal Report rendered differently on server and development machine
  • Excel formula that copies cell content while removing text
  • Transforming Calendar Quarter to Financial Quarter
  • How to add custom action button in Django admin form and post the information
  • Inheritance and lazy loading in NHibernate
  • Ternary Best Practice
  • Binding a combobox in subform Access
  • VBS To Event Log
  • Extract all URLs that start with http or https and end with html from text file
  • Use a Batch File to list files and allow the user to select which file to copy into a new destinatio
  • Calling JavaScript function in WebView
  • How to add a CAGradientLayer to a UIView when using programmatic constraints
  • MySQL: Advisable number of rows
  • Objects Sorting With date ,Time Problem in Array(Iphone Development)
  • Change the font based on language CSS
  • How to use CoreFoundation in QuickTime SDK for Windows?
  • Enable CORS on Tomcat 8.0.30
  • Node.js - Why does my callback get called 3 times for each request?
  • Backbone - Create Multiple Models in Collection - serverside
  • Remove stopwords and tolower function slow on a Corpus in R
  • Admob in ListView not clickable
  • JavaMail connection problems [duplicate]
  • Getting the type of a “Type” in C# reflection
  • Add font awesome icon to custom add to cart button in Woocommerce 3
  • how to read to huge file into buffer
  • Using redis as an LRU cache for postgres
  • How to handle div that is created dynamically in a table
  • Did not understand process of initialize in swift programming
  • Google App Engine backend servlet not responding