Saturday, 29 April 2023

7 Cool Ways To Use F-Strings In Python


 

1. Align strings with f-strings:

You can use f-strings to align strings to the left, right, or center of a field. Here's an example:

name = "Alice"

age = 30

print(f"|{name:<10}|{age:^5}|")  # Output: |Alice     | 30  |

In this example, the < character aligns the name variable to the left of a 10-character field, and the ^ character centers the age variable in a 5-character field.


2. Use f-strings with dictionary variables:

You can use f-strings with dictionary variables to create dynamic strings. Here's an example:

person = {"name": "Alice", "age": 30}

print(f"My name is {person['name']} and I'm {person['age']} years old.")  # Output: My name is Alice and I'm 30 years old.

In this example, the person variable is a dictionary with keys "name" and "age". The f-string uses the values of these keys to create a dynamic string.


3. Use f-strings to format binary and hexadecimal numbers:

You can use f-strings to format binary and hexadecimal numbers. Here's an example:

x = 42

print(f"x = {x:b}")  # Output: x = 101010

print(f"x = {x:x}")  # Output: x = 2a

In this example, the :b format specifier formats the x variable as a binary number, and the :x format specifier formats the x variable as a hexadecimal number.


4. Use f-strings to format dates and times:

You can use f-strings to format dates and times. Here's an example:

import datetime

now = datetime.datetime.now()

print(f"Today is {now:%B %d, %Y}")  # Output: Today is April 29, 2023

In this example, the %B %d, %Y format specifier formats the now variable as a string in the format Month Day, Year.


5. Use f-strings to format currency values:

You can use f-strings to format currency values. Here's an example:

salary = 50000

print(f"My salary is ${salary:,}")  # Output: My salary is $50,000

In this example, the , character formats the salary variable as a string with comma separators.


6. Use f-strings with formatted strings:

You can use f-strings with formatted strings to create complex strings. Here's an example:

name = "Alice"

age = 30

message = f"My name is {name} and I'm {age} years old."

print(f"Message length: {len(message):<10}, Message: '{message:^20}'")

# Output: Message length: 32        , Message: 'My name is Alice and I'm 30 years old.'

In this example, the f-string uses another f-string to create a complex string that includes the length of the message variable and the message itself.


7.Use f-strings to format scientific notation:

You can use f-strings to format numbers in scientific notation. Here's an example:

x = 1234567890.123456789

print(f"x = {x:e}")  # Output: x = 1.234568e+09

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (23) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses