diff --git a/python.md b/python.md index 8d16f3f..8ec32ed 100644 --- a/python.md +++ b/python.md @@ -145,10 +145,22 @@ counter = 0 counter = counter + 10 # => 10 message = "Part 1." + # => Part 1.Part 2. message += "Part 2." ``` +### f-Strings (Python 3.6+) +```python +>>> website = 'Quickref.ME' +>>> f"Hello, {website}" +"Hello, Quickref.ME" + +>>> num = 1num = 100 +>>> f'{num} + 10 = {num + 10}' +'10 + 10 = 20' +``` +See: [Python F-Strings](/python#python-f-strings-since-python-3-6) @@ -436,6 +448,112 @@ Get input data from console True ``` +Python F-Strings (Since Python 3.6+) {.cols-3} +---------------- + + +### f-Strings usage +```python +>>> website = 'Quickref.ME' +>>> f"Hello, {website}" +"Hello, Quickref.ME" + +>>> num = 1num = 100 +>>> f'{num} + 10 = {num + 10}' +'10 + 10 = 20' + +>>> f"""He said {"I'm John"}""" +"He said I'm John" + +>>> f'5 {"{stars}"}' +'5 {stars}' +>>> f'{{5}} {"stars"}' +'{5} stars' + +>>> name = 'Eric' +>>> age = 27 +>>> f"""Hello! +... I'm {name}. +... I'm {age}.""" +"Hello!\n I'm Eric.\n I'm 27." +``` +it is available since Python 3.6, also see: [Formatted string literals](https://docs.python.org/3/reference/lexical_analysis.html#f-strings) + + + +### f-Strings Fill Align +```python +>>> f'{"text":10}' # [width] +'text ' +>>> f'{"test":*>10}' # fill left +'******test' +>>> f'{"test":*<10}' # fill right +'test******' +>>> f'{"test":*^10}' # fill center +'***test***' +>>> f'{12345:0>10}' # fill with numbers +'0000012345' +``` + + +### f-Strings Type +```python +>>> f'{10:b}' # binary type +'1010' +>>> f'{10:o}' # octal type +'12' +>>> f'{200:x}' # hexadecimal type +'c8' +>>> f'{200:X}' +'C8' +>>> f'{345600000000:e}' # scientific notation +'3.456000e+11' +>>> f'{65:c}' # character type +'A' +>>> f'{10:#b}' # [type] with notation (base) +'0b1010' +>>> f'{10:#o}' +'0o12' +>>> f'{10:#x}' +'0xa' +``` + + +### F-Strings Others +```python +>>> f'{-12345:0=10}' # negative numbers +'-000012345' +>>> f'{12345:010}' # [0] shortcut (no align) +'0000012345' +>>> f'{-12345:010}' +'-000012345' +>>> import math # [.precision] +>>> math.pi +3.141592653589793 +>>> f'{math.pi:.2f}' +'3.14' +>>> f'{1000000:,.2f}' # [grouping_option] +'1,000,000.00' +>>> f'{1000000:_.2f}' +'1_000_000.00' +>>> f'{0.25:0%}' # percentage +'25.000000%' +>>> f'{0.25:.0%}' +'25%' +``` + + +### F-Strings Sign +```python +>>> f'{12345:+}' # [sign] (+/-) +'+12345' +>>> f'{-12345:+}' +'-12345' +>>> f'{-12345:+10}' +' -12345' +>>> f'{-12345:+010}' +'-000012345' +``` Python Lists {.cols-3} ------------