Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
usage of ^ in f_string
#1
hi
in the below code:
# normal pyramid
print("\nNormal Pyramid")
for i in range(5):
    x="* "
    x=x*i
    print(f'{x:^10}')
what is ^10 in last print line?also {x:<10} and {x:>10} can be used. what are they?
thanks
Reply
#2
the ":^10" in an f-string will center value of 'x' in a 10 character field.
akbarza likes this post
Reply
#3
< will left justify. > right justify, ^ center justify
print('0123456789')
value = "Hi"
print(f"{value}")
print(f"{value:<10}")
print(f"{value:>10}")
print(f"{value:^10}")
Output:
0123456789 Hi Hi Hi Hi
You don't often see "<" used.
akbarza likes this post
Reply
#4
hi
what does !r do in below code?
fpath = "C:\new\text\sample.txt"
print(f'this is a test and {fpath}')
# output: this is a test and C:
# ew	ext\sample.txt
print(f'this is a test and {fpath!r}')
# out: this is a test and 'C:\new\text\\sample.txt'
fpath = r"C:\new\text\sample.txt"
print(f'this is a test and {fpath}')
# output: this is a test and C:\new\text\sample.txt
print(f'this is a test and {fpath!r}')
# output: this is a test and 'C:\\new\\text\\sample.txt'
thanks
Reply
#5
It's always a good idea to read documentation: Format String Syntax and Format Specification Mini-Language

Quote:Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().
buran and akbarza like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Nov-07-2023, 08:51 AM)perfringo Wrote: It's always a good idea to read documentation: Format String Syntax and Format Specification Mini-Language

Quote:Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().

hi
I knew that I must explore for f-string, but I did not know where( in which internet address) I must search.
can you tell me where I must search when I encounter an ambiguous subject?
thanks
Reply
#7
Search is very wide subject and "how" depends on very many factors.

For Python I personally follow this pattern:

1 Search Python built-in help
2. Search Python documentation at python.org
3. Search internet with Google

This specific subject was about string formatting so following should be pretty self-explanatory:

>>> help('format')
Help on built-in function format in module builtins:

format(value, format_spec='', /)
    Return value.__format__(format_spec)

    format_spec defaults to the empty string.
    See the Format Specification Mini-Language section of help('FORMATTING') for
    details.

>>> # very helpful hint what to search
>>> help('FORMATTING')
# oh boy, this will spill out lengthy and quite comprehensive information
akbarza likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(Nov-09-2023, 01:15 PM)perfringo Wrote: Search is very wide subject and "how" depends on very many factors.

For Python I personally follow this pattern:

1 Search Python built-in help
2. Search Python documentation at python.org
3. Search internet with Google

This specific subject was about string formatting so following should be pretty self-explanatory:

>>> help('format')
Help on built-in function format in module builtins:

format(value, format_spec='', /)
    Return value.__format__(format_spec)

    format_spec defaults to the empty string.
    See the Format Specification Mini-Language section of help('FORMATTING') for
    details.

>>> # very helpful hint what to search
>>> help('FORMATTING')
# oh boy, this will spill out lengthy and quite comprehensive information

thanks very much
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020