This question already has answers here:
How to print variables without spaces between values [duplicate]
(6 answers)
Closed 17 days ago.
I am trying to convert inches into height and inches using the feet'inches format yet I cant seem to get rid of the whitespace in-between the characters.
height1 = height // 12
height2 = height % 12
print(height1, '\', height2)
this is my current code
5 ' 9 this is the output
5'9 i need this to be the output
You can use string concatenation directly or an f-string.
print(f"{height1}'{height2}")
Related
This question already has answers here:
Printing Lists as Tabular Data
(20 answers)
Python spacing and aligning strings
(7 answers)
Create nice column output in python
(22 answers)
How to Print "Pretty" String Output in Python
(5 answers)
Closed 1 year ago.
I'm trying to create something like this as terminal output:
VALUE A VALUE B INT C
alpha bravo 69420
charlie delta 69421
echo foxtrot 69422
I have a snippet of code from a different script to print the correct number of spaces (in this example, make it so that there are spaces after the printed value until col 64.)
print(header)
for f in files:
fLen = len(f)
fiLen = (fLen - 64)
finLen = (fiLen - fiLen - fiLen)
print(f+" "*(fiLen))
I'm not sure how to:
Get this same operation done with less operation to keep it "pythonic",
Scale this to multiple columns (like to automatically generate a lot more rows of that example output.
This question already has answers here:
Replacing instances of a character in a string
(17 answers)
Closed 4 years ago.
How can I take a string that returns it in a list with " / " between them, like dates.
for example
Taking 5,11,2013 and the output be 5/11/2013
Use str.replace() ?
date = '11,12,2019'
print(date.replace(',', '-'))
>> 11-12-2019
This question already has answers here:
How do you print superscript in Python?
(13 answers)
Closed 4 years ago.
I'm doing a maths revision program and I want to print out a question with y to the power of x(like xⁿ). I can't find a way to print it out as this form. Has anyone got any ideas?
This is how I want it to be displayed.
One possible option using ANSI escape sequences, it is not possible to control font size:
base = "x"
exponent = "n"
print('\033[1BFormula = %s\033[1A%s\033[1B etcetera'%(exponent, base))
In terminal you get:
# x
#Formula = n etcetera
This question already has answers here:
How do I pad a string with zeroes?
(19 answers)
Closed 4 years ago.
How to input a number from 0 to 1000 and have python return a string that is always 4 characters long.
for example: input 10 python should return 0010
You can use zero padding format with fixed width:
"{:04d}".format(number)
You can do something like this, Hope this helps
num = 10
if num < 1000:
print "%04d" % (num,)
This question already has answers here:
How to convert a string of space- and comma- separated numbers into a list of int? [duplicate]
(6 answers)
How to split a string of space separated numbers into integers?
(9 answers)
Closed 4 years ago.
I am trying to take in a string of numbers, such as:
1 2 3 4 55 33 15
and store them in a list as they appear so that I can operate on the items at each index in the list, but they aren't splitting and storing right. They store like:
1,2,3,4,5,5,3,3,1,5
which is not what I want. My current code is attached below. I thought they would store as:
1,2,3,4,5,55,33,15
I do convert them to ints later on and that works fine, this is just the part that I am stuck on.
numbersArrayTemp.append(userInput.readline().strip(' ').strip('\n'))
for item in numbersArrayTemp:
print(item)
Type = item.strip('\n')
print(Type)
j = item.replace(" ", '')
numbersArrayString.extend(j)