Add spaces at the beginning of the print output in python - python

I'm wondering how Am I suppose to add 4 spaces at the beginnings of the print outputs with f-string or format in python?
This is what I use to print now:
print('{:<10}'.format('Constant'),'{:{width}.{prec}f}'.format(122, width=10, prec=3))
and my output look like this:
Constant 122.000
but what I want is to have 4 spaces before the constant in the output like:
( 4 spaces here )Constant 122.000
Any ideas? Thanks a lot!

You can use ' ' + string (as suggested), but a more robust approach could be:
string="Test String leading space to be added"
spaces_to_add = 4
string_length=len(string) + spaces_to_add # will be adding 4 extra spaces
string_revised=string.rjust(string_length)
result:
' Test String leading space to be added'

There's a couple ways you could do it:
If Constant is really an unchanging constant, why not just
print(f" {Constant}", ...)
before your other string?
With your current implementation, you are left-aligning to a width of 10 characters. If you swap that to right-align, like '{:>12}'.format('Constant') ("Constant" is 8 characters, 12 - 8 = 4 spaces) It will put 4 characters in front of the string.
Here's a Python f-string syntax cheat sheet I've used before:
https://myshell.co.uk/blog/2018/11/python-f-string-formatting-cheatsheet/
And the official docs: PEP 3101

Related

Want to replace comma with decimal point in text file where after each number there is a comma in python

eg
Arun,Mishra,108,23,34,45,56,Mumbai
o\p I want is
Arun,Mishra,108.23,34,45,56,Mumbai
Tried to replace the comma with dot but all the demiliters are replaced with comma
tried text.replace(',','.') but replacing all the commas with dot
You can use regex for these kind of tasks:
import re
old_str = 'Arun,Mishra,108,23,34,45,56,Mumbai'
new_str = re.sub(r'(\d+)(,)(\d+)', r'\1.\3', old_str, 1)
>>> 'Arun,Mishra,108.23,34,45,56,Mumbai'
The search pattern r'(\d+)(,)(\d+)' was to find a comma between two numbers. There are three capture groups, therefore one can use them in the replacement: r\1.\3 (\1 and \3 are first and third groups). The old_str is the string and 1 is to tell the pattern to only replace the first occurrence (thus keep 34, 45).
It may be instructive to show how this can be done without additional module imports.
The idea is to search the string for all/any commas. Once the index of a comma has been identified, examine the characters either side (checking for digits). If such a pattern is observed, modify the string accordingly
s = 'Arun,Mishra,108,23,34,45,56,Mumbai'
pos = 1
while (pos := s.find(',', pos, len(s)-1)) > 0:
if s[pos-1].isdigit() and s[pos+1].isdigit():
s = s[:pos] + '.' + s[pos+1:]
break
pos += 1
print(s)
Output:
Arun,Mishra,108.23,34,45,56,Mumbai
Assuming you have a plain CSV file as in your single line example, we can assume there are 8 columns and you want to 'merge' columns 3 and 4 together. You can do this with a regular expression - as shown below.
Here I explicitly match the 8 columns into 8 groups - matching everything that is not a comma as a column value and then write out the 8 columns again with commas separating all except columns 3 and 4 where I put the period/dot you require.
$ echo "Arun,Mishra,108,23,34,45,56,Mumbai" | sed -r "s/([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)/\1,\2,\3.\4,\5,\6,\7,\8/"
Arun,Mishra,108.23,34,45,56,Mumbai
This regex is for your exact data. Having a generic regex to replace any comma between two subsequent sets of digits might give false matches on other data however so I think explicitly matching the data based on the exact columns you have will be the safest way to do it.
You can take the above regex and code it into your python code as shown below.
import re
inLine = 'Arun,Mishra,108,23,34,45,56,Mumbai'
outLine = re.sub(r'([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)'
, r'\1,\2,\3.\4,\5,\6,\7,\8', inLine, 0)
print(outLine)
As Tim Biegeleisen pointed out in an original comment, if you have access to the original source data you would be better fixing the formatting there. Of course that is not always possible.
First split the string using s.split() and then replace ',' in 2nd element
after replacing join the string back again.
s= 'Arun,Mishra,108,23,34,45,56,Mumbai '
ls = s.split(',')
ls[2] = '.'.join([ls[2], ls[3]])
ls.pop(3)
s = ','.join(ls)
It changes all the commas to dots if dot have numbers before and after itself.
txt = "2459,12 is the best number. lets change the dots . with commas , 458,45."
commaindex = 0
while commaindex != -1:
commaindex = txt.find(",",commaindex+1)
if txt[commaindex-1].isnumeric() and txt[commaindex+1].isnumeric():
txt = txt[0:commaindex] + "." + txt[commaindex+1:len(txt)+1]
print(txt)

Unwanted spaces added in print function call

Number of characters in yellow_Daisy : 12
Number of characters in 6yellow6 : 8
how can I move the : closer to the words.
here is what I wrote but I don't know how to get read of the space
print('Number of characters in', password1,':', len(password1))
print('Number of characters in', password2,':', len(password2))
f-strings to the rescue.
print(f'Number of characters in {password1}: {len(password1)}')
print(f'Number of characters in {password2}: {len(password2)}')
With f-strings (python 3.6 and later), you can put python expressions in curly braces. Now you have a single string with the spacing and formatting you want, and {..} will be replaced with the evaluation of the expression.
try formatting the string as such:
print("Number of characters in {a}:{b}".format(a=password1, b=len(password1)))
or
print(f"Number of characters in {password1}:{len(password1)}")
I think tdelaney has provided a great answer. Another simpler way (but not so clear as f-strings) is to use concatenation (+) instead of commas, e.g.
print('Number of characters in' + password1 + ':' + str(len(password1)))

Formatting a line with spaces at the beginning

Consider below piece of code
line = "I am writing a question"
print('{0: >10}'.format(line))
This does not work as expected. I expected output to be
' I am writing a question'
I know I can achieve this by other means like printing the spaces first using one print statement and then print the sentence. But curious to know what I might be doing wrong.
Your line is longer than 10 characters; the width is a minimal value and applies to the whole column. If you wanted to add 10 spaces, always, prefix these before the format:
print(' {0}'.format(line))
If you always wanted to right-align the string in a column of 33 characters (10 spaces and 23 characters for your current line), then set the column width to that instead:
print('{0:>33}'.format(line))
Now, when your line value is longer or shorter, the amount of whitespace will be adjusted to make the output 33 characters wide again.
Demo:
>>> line = "I am writing a question"
>>> print(' {0}'.format(line))
I am writing a question
>>> print('{0:>33}'.format(line))
I am writing a question
>>> line = "question"
>>> print('{0:>33}'.format(line))
question
There is a built in method :
https://docs.python.org/2/library/stdtypes.html#str.rjust
v = 'hi'
print v.rjust(4, ' ');
prints
' hi'

Aligning output in Python?

Is there a way where I can right-align my output in such a way like this:
Item: $ 13.69
Tax: $ 5.30
Oth: $ 2.50
---------------
Total: $ 99.80
Please note that I am using Python 3.
You can use the .format method of strings to do this:
fmt = '{0:>5}: ${1:>6.2f}'
print(fmt.format('Item', 13.69)) # Prints ' Item: $ 13.69'
print(fmt.format('Tax', 5.3))
print(fmt.format('Oth', 2.5))
print('-'*len(fmt.format('Item', 13.69))) # Prints as many '-' as the length of the printed strings
print(fmt.format('Total', 99.8))
# etc...
The '{0:>5}' part is saying "take the zeroth item given to .format, and right justify it within 5 spaces". The '{1:>6.2f}' part is saying take the first item given to .format, right justify it within 6 spaces, formatting as a decimal with 2 decimal places.
Of course, in real code this would likely be part of a loop.
Use string formatting:
print("$%6s" % dec_or_string)
You can use the same amount of spaces before the text with all of the items you want to align.
Another option would be to use the str.format operator as explained here.

Can difflib's charjunk be used to ignore whitespace?

I'd like to compare differences between two lists of strings. For my purposes, whitespace is noise and these differences do not need to be shown. Reading into difflib's documentation, "the default [for charjunk] is module-level function IS_CHARACTER_JUNK(), which filters out whitespace characters". Perfect, except I don't see it working, or making much difference (<- pun!).
import difflib
A = ['3 4\n']
B = ['3 4\n']
print ''.join(difflib.ndiff(A, B)) # default: charjunk=difflib.IS_CHARACTER_JUNK
outputs:
- 3 4
? -
+ 3 4
I've tried a few other linejunk options, but none that actually ignore the differences as a result of whitespace. Do I have the wrong interpretation for what charjunk is for?
As a side note, I can side-step this limitation by pre-processing my strings to substitute multiple whitespace characters to single space characters using re.sub(r'\W+', ' ', 'foo\t bar').

Categories

Resources