Posts

Showing posts from July, 2024

F-string in Python

 ' f-string ' Formatting Python f-strings are a simple and efficient way to format strings. The f-strings make it easy to embed variables directly into a string. Introduced in Python version 3.6 in late 2016, f-strings significantly changed the conventional method of using separate formats for different data types such as floats, integers, and strings.  Here's the basics: How to Use : Start your string with an f or F before the quotation marks. Inside the string, put any variables or expressions you want to use inside curly braces {} . name = "Romanch" age = 25 print(f"My name is {name} and I am {age} years old.") Output: My name is Romanch and I am 25 years old. Why f-strings? Readable : It’s easy to see what the final string will look like, and the code is concise. Efficient : f-strings are faster than other formatting methods; like format() or % . Flexible : You can include expressions and calculations inside {} , not just variables. x = 5 y = 10 ...