Put a Right-to-Left Embedding character, u'\u202B', at the beginning of each Hebrew word, and a Pop Directional Formatting character, u'\u202C', at the end of each word.
This will set the Hebrew words apart as RTL sections in an otherwise LTR document.
(Note that while this will produce the correct output, you're also dependent on the terminal application in which you're running this script having implemented the Unicode Bidirectional Algorithm correctly.)
Answer from kpozin on Stack OverflowPut a Right-to-Left Embedding character, u'\u202B', at the beginning of each Hebrew word, and a Pop Directional Formatting character, u'\u202C', at the end of each word.
This will set the Hebrew words apart as RTL sections in an otherwise LTR document.
(Note that while this will produce the correct output, you're also dependent on the terminal application in which you're running this script having implemented the Unicode Bidirectional Algorithm correctly.)
See Bi-directional (BiDi) layout implementation in pure python.
Install with:
pip install python-bidi
Example usage:
from bidi.algorithm import get_display
print(get_display('LTR text with RTL text (טקסט לדוגמא) will be printed correctly'))
The following package is also available if you are using Django: http://pypi.python.org/pypi/django-bidi-utils
Try this approach using the newer str.format syntax. This uses a width of 12, space padded, right aligned.
line_new = '{:>12} {:>12} {:>12}'.format(words[0], words[1], words[2])
Example with the interpreter:
>>> line = "123 456 789"
>>> words = line.split(' ')
>>> line_new = '{:>12} {:>12} {:>12}'.format(words[0], words[1], words[2])
>>> print(line_new)
123 456 789
And here's how to do it using the old % syntax (useful for older versions of Python that don't support str.format):
line_new = '%12s %12s %12s' % (words[0], words[1], words[2])
Here is another way how you can format using 'f-string' format:
print(
f"{'Trades:':<15}{cnt:>10}",
f"{'Wins:':<15}{wins:>10}",
f"{'Losses:':<15}{losses:>10}",
f"{'Breakeven:':<15}{evens:>10}",
f"{'Win/Loss Ratio:':<15}{win_r:>10}",
f"{'Mean Win:':<15}{mean_w:>10}",
f"{'Mean Loss:':<15}{mean_l:>10}",
f"{'Mean:':<15}{mean_trd:>10}",
f"{'Std Dev:':<15}{sd:>10}",
f"{'Max Loss:':<15}{max_l:>10}",
f"{'Max Win:':<15}{max_w:>10}",
f"{'Sharpe Ratio:':<15}{sharpe_r:>10}",
sep="\n"
)
This will provide the following output:
Trades: 2304
Wins: 1232
Losses: 1035
Breakeven: 37
Win/Loss Ratio: 1.19
Mean Win: 0.381
Mean Loss: -0.395
Mean: 0.026
Std Dev: 0.56
Max Loss: -3.406
Max Win: 4.09
Sharpe Ratio: 0.7395
What you are doing here is you are saying that the first column is 15 chars long and it's left-justified and the second column (values) is 10 chars long and it's right-justified.
If you joining items from the list and you want to format space between items you can use `` and regular formatting techniques.
This example separates each number by 3 spaces. The key here is f"{'':>3}"
print(f"{'':>3}".join(str(i) for i in range(1, 11)))
output:
1 2 3 4 5 6 7 8 9 10
I think you can use sys.stdout for this:
import sys
def stdout(message):
sys.stdout.write(message)
sys.stdout.write('\b' * len(message)) # \b: non-deleting backspace
def demo():
stdout('Right'.rjust(50))
stdout('Left')
sys.stdout.flush()
print()
demo()
You can replace 50 with the exact console width, which you can get from https://stackoverflow.com/a/943921/711085
Here is a pretty simple method:
>>> left, right = 'Left', 'Right'
>>> print '|{}{}{}|'.format(left, ' '*(50-len(left+right)), right)
|Left Right|
As a function:
def lr_justify(left, right, width):
return '{}{}{}'.format(left, ' '*(width-len(left+right)), right)
>>> lr_justify('Left', '', 50)
'Left '
>>> lr_justify('', 'Right', 50)
' Right'
>>> lr_justify('Left', 'Right', 50)
'Left Right'
>>> lr_justify('', '', 50)
' '
You can prefix the size requirement with - to left-justify:
sys.stdout.write("%-6s %-50s %-25s\n" % (code, name, industry))
This version uses the str.format method.
Python 2.7 and newer
sys.stdout.write("{:<7}{:<51}{:<25}\n".format(code, name, industry))
Python 2.6 version
sys.stdout.write("{0:<7}{1:<51}{2:<25}\n".format(code, name, industry))
UPDATE
Previously there was a statement in the docs about the % operator being removed from the language in the future. This statement has been removed from the docs.
n = 5
print(*[' '.join(' '*i + '*'*(n-i)) for i in range(n)], sep='\n')
Output:
* * * * *
* * * *
* * *
* *
*
Explanation:
for i in range(n):
chars = ' '*i + '*'*(n-i) # creating list of (i) spaces followed
# by (n-i) stars to finish a line of n elements
print(' '.join(chars)) # join prepared values with spaces
Here is a simple solution not using list comprehension:
n = 5
for i in range(n+1):
for j in range(i):
print(" ", end="")
for j in range(i+1, n+1):
print("* ", end="")
print()
Output:
* * * * *
* * * *
* * *
* *
*