the tab character in python really confuse me [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i = 1
while i <= 6:
print 2**i,' ','\t', 3**i
i += 1
print
i = 1
while i <= 6:
print 2**i ,'\t', 3**i
i += 1
print
This is the two different code I wrote. For the first one, I add four spaces and the output is strange. If I change the four spaces into three spaces, the output of the two codes will be the same. I don't understand it.
Output:

If you want fixed, formatted output you should look into formatted print statements instead of using tabs. This site has a good description: https://pyformat.info/ The padding and aligning section is probably what you want to start with to help clean up your output.

Related

Math program Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I'm struggling to implement a program that prompts the user for an arithmetic expression,
(for example; 1 + 1) and then calculates and outputs the result as a floating-point value formatted to one decimal place (for example; 2.0).
Also, I want there to be a space between x and y, and a space between y and z in the equation (for example; 1 + 1, with spaces between each character).
This is basically my desired output (in a terminal):
https://i.stack.imgur.com/5LuRH.png
I'm pretty new to python so this will probably seem easier to more experienced coders, but anyone have any ideas, and thanks in advance.
You can just simply use eval directly on the expression inserted...
expression = str(input('Expression: '))
print("{:.1f}".format(eval(expression), 1))
output:
Expression: 1 + 1
2.0

Destroy 2 caracters after a number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).

how can I solve this in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False

How to using the "for" loop in python make the output print in one line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a simple bit of code what using the loop "for" prints out all the leters from input. It looks something like this:
word = str(input('Word here:'))
for i in range(0, len(word), 1):
print(word[i])
I would really like sme help
You can end keyword in print statement to remove the newline character.
print(word[i],end=" ")

How can I eliminate typos from being logged in a keylogger? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Ideally, I'd like the program to erase/un-log any and all erroneous character(s) when the Backspace key is pressed and replace them with the correct characters.
After searching for solutions to no avail, I'm wondering if it's even possible? If it is, my guess is that the code needed to do this might involve the modules: 're', 'readchar', 'msvcrt', 'getch' or some combination of those, in addition to using 'string.replace', 'x.remove', 'r/R', 'raw_input' 'x.translate', or the like. But I don't have the knowledge or skills yet to figure out how to apply them.
This code may be what you are looking for:
import re
text = "Helll[Back Space]o how are yoo[Back Space]u"
result = list(text)
for (start, end) in [(m.start(), m.end()) for m in re.finditer('\[Back Space\]', text)]:
text = text.replace(''.join(result[start-1:end]), '')
print text
Output:
Hello how are you

Categories

Resources