What is the tab key in python?

I'm working on a Python application that asks the user for two inputs, but in order for them to go to the next input, they need to hit ENTER/RETURN, is it possible to also use the TAB key?

Right now, if the user hits TAB, it'll just add a space to the text.

[EDIT] I did try the sys.stdin method but all I get is a blank screen.

asked Nov 23, 2015 at 10:23

What is the tab key in python?

John LisboaJohn Lisboa

731 gold badge2 silver badges12 bronze badges

2

As stated here, if you use raw_input or input it is not possible and you would have to read from stdin directly

import sys


def input_with_tab():
    text = ""
    while True:
        char = sys.stdin.read(1)  # read 1 character from stdin
        if char == '\t':  # if a tab was read
            break
        text += char

    return text

print("Write something:")
text = input_with_tab()
print(text)

answered Nov 23, 2015 at 10:28

What is the tab key in python?

JDurstbergerJDurstberger

4,0176 gold badges29 silver badges66 bronze badges

5

The Tab Key

Index of All Documentation » Wing Pro Reference Manual » Source Code Editor » Indentation »

The action of the tab key depends on the Editor > Keyboard > Personality preference, the file type being edited, and the position within the file.

To insert a real tab character, press Ctrl-T.

Tab Key Action

The behavior of the tab key can be altered with the User Interface > Keyboard > Tab Key Action preference, which provides the following options:

Default for Personality selects from the other tab key actions below, according to the current keyboard personality and file type. In all non-Python files, the default is Move to Next Tab Stop. In Python files, the defaults are as follows:

Normal: Smart Tab

VI/VIM: Move to Next Tab Stop

Emacs: Indent to Match

Brief: Smart Tab

Visual Studio: Move to Next Tab Stop

macOS: Smart Tab

Eclipse: Emulates Eclipse

XCode: Smart Tab

MATLAB: Insert Tab Character

Indent to Match indents the current line or selected lines to position them at the computed indent level for their context in the file.

Move to Next Tab Stop enters indentation so that the caret reaches the next tab stop.

Indent Region increases the indentation of the current line or selected lines by one level.

Insert Tab Character inserts a Tab character chr(9) into the file.

Smart Tab is equivalent to Move to Next Tab Stop in non-Python files, and implements the following behavior in Python files:

(1) When the caret is within a line or there is a non-empty selection, this performs Indent to Match. When the line or lines are already at the matching position, indentation is toggled between other valid positions.

(2) When the caret is at the end of a non-empty line and there is no selection, one indent level is inserted. The User Interface > Keyboard > Smart Tab End of Line Indents preference alters the type of indentation used in this case, or disables this aspect of the Smart Tab feature.

For programmers, in fact, tabs and spaces are far more than just a question of "position".

The length of the tab may be inconsistent in different editors, so after setting indentation with tab in one editor, the indentation may be messed up in other editors. This problem does not occur with spaces, because spaces occupy one character position.

As we all know, Tab in ASCII code, the code is 9, and the space is 32. This means that when we press a Tab, even if it looks like 8 spaces (or 4 spaces, in different environments, Tab may display different effects), it is completely different to the computer. The same thing. This also means that for codes that use characters to describe the process, it is most likely to be a decisive difference.

Especially for a language that uses space indentation to distinguish code levels-Python.

Let's look at a piece of code.

classMyForm(Form):
 value1 =StringField('value1')
 value2 =StringField('value2')
 value3 =StringField('value3')  #This line uses Tab indentation
 submit =SubmitField('Submit')
 learn python ='QQ group:725479218'

It seems that this value3 variable is no different from other variables, but there is such an error-indentation error.

value3 =StringField('value3')
IndentationError: unexpected indent

In fact, Python does not force you to use Tab indentation or space indentation, and even the number of spaces is not mandatory, but it is absolutely! Tabs and spaces must not be mixed, so here, is there a big difference between spaces and tabs?

At this time, some children's shoes are about to be mentioned. Why have I never had such a problem with PyCharm (or other IDE)?

In fact, many IDEs have made various optimizations for the Tab key. One of them is to expand the Tab key to a space. That is to say, when you press Tab, the IDE actually helps you put a "9 "Is converted into four (or eight) "32". But be aware that not all IDEs do this kind of work for you! In the same way, for the editor Vim, which is so pure and unpretentious, it will definitely not help you do this kind of work.

Since Tab displays differently in different environments, the spaces are always the same. For some detailed typesetting and indentation (for example, if you want to align each line of comments), using spaces is more precise. It seems that using spaces to write code is better than using Tab.

**The benefits of spaces instead of Tab: **

The code is what you want in all cases. The tab only looks good when you and the code author's tab size is set to the same. Modifying the tab size does not solve this problem, because it is difficult for you to modify the tab size every time you open a file, and everyone usually has different habits (the POSIX/Unix standard tab should be 8 characters wide, Linus also stipulated The tab size of all codes in the Linux kernel is 8). If there are end-of-line comments, the tab size must be set to be the same as the author, which means that you need to modify the tab size frequently when you look at different codes. I have seen many codes, and the tab sizes used are from 2, 3, 4, 5, 6, 8, 16 or even 32. If you use a tab size different from the author, the appearance will be very undesirable.

Reliable IDEs can solve the problem of increasing and reducing indents in forward and backward directions. Even if it is four spaces, a backspace key can also retreat, so there is no problem in the convenience of use. ——If you complain that deleting adjustments cannot be effectively resolved, you need to study your editor. In fact, there are shortcut keys for increasing or decreasing indentation directly in mainstream editors. No matter it is tab or space or backspace, it is rarely used directly for indentation.

tab is a tab character rather than an indentation character, as it is used extensively in html pages <table 进行布局是个不好的编程习惯一样,在编程中大量使用制表符布局通常也不是个好习惯。

Under normal circumstances, team development must formulate a set of coding standards. In most teams, using 4 spaces instead of Tab is the default. Therefore, it is highly recommended that you use spaces instead of Tab. In addition, each IDE (editor) provides the function of automatically converting spaces into tabs. As long as you set it up, you can press the tab key to display 4 or more spaces.

Content expansion:

Use of Python_Tab key

  1. When entering an expression in the shell, as long as you press the Tab key, any variable (object, function, etc.) in the current namespace that matches the entered string will be found:
In[1]: an_orange =27
In[2]: an_pear =15
In[3]:an(press<Tab )
anorange an_pear and any
  1. Enter a period after any object to automatically complete the input of methods and properties:
In[1]: a =[1,2,3]
In[2]: a.<Tab 
a.append a.extend a.remove a.sort
a.count a.index a.pop a.reserve
  1. Applied to the module:
In[1]:import pandas
In[2]: pandas.<Tab 
pandas.cut pandas.core pandas.concat
  1. When entering anything that looks like a file path (even in a Python string), press the Tab key to find out what matches it in the computer's file system.

  2. The Tab key auto-completion function can be used for function keyword parameters.

So far, this article on what the tab key in python means is introduced here. For more related content on what the tab key in python means, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you all Support ZaLou.Cn a lot!

Which is the tab key?

Where is the tab key on the keyboard? You can find the tab key on the left side of the keyboard, just above the caps lock key and to the left of the Q key. You can recognise the tab key by its two arrows going in opposite directions and pointing towards a line, one above the other.

How do you say tab in Python?

The '\' backslash in Python strings is a special character, sometimes called the escape character. It is used to represent whitespace characters as '\t' represents a tab.

What is tab key example?

Pressing Windows key + Tab shows available open programs in Microsoft Windows. In most programs and computers, pressing the Tab key moves between selectable elements. For example, you press the Tab key in your Internet browser now to switch between all selectable elements on this web page.

What is the Ascii code for tab?

Character Name
Char
Decimal
Back Space
BS
8
Horizontal Tab
TAB
9
Line Feed
LF
10
Vertical Tab
VT
11
ASCII Character Chart with Decimal, Binary and Hexadecimal Conversionswww.eso.org › ~ndelmott › asciinull