Open In App

New ‘=’ Operator in Python3.8 f-string

Last Updated : 03 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Python have introduced the new = operator in f-string for self documenting the strings in Python 3.8.2 version. Now with the help of this expression we can specify names in the string to get the exact value in the strings despite the position of the variable. Now f-string can be defined as f'{expr=}' expression. We can specify any required name in place of expr.

Syntax : f'{expr=}'
Return : Return the formatted string.

Note: For older versions of Python this operator will raise the syntax error. See the below image.

Example #1 :
In this example we can see that with the help of f'{expr=}' expression, we are able to format strings in python by self documenting expressions by using = operator.




length = len('GeeksForGeeks')
  
# Using f'{expr =}' expression
gfg = f'The length of GeeksForGeeks is {length =}.'
  
print(gfg)


Output :

The length of GeeksForGeeks is length=13.

Example #2 :




a, b = 5, 10
  
# Using f'{expr =}' expression
gfg = f'Value of {b = } and {a = }.'
  
print(gfg)


Output :

Value of b = 10 and a = 5.


Previous Article
Next Article

Similar Reads

Python3 Program to Check if a string can be formed from another string by at most X circular clockwise shifts
Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a" - Shift 3 times - "d" Character "b" - Shift 2 ti
3 min read
Python3 Program for Check if a string can be obtained by rotating another string d places
Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str2 = "cdfdawb", d = 6 Output: No Approach: An appro
3 min read
Python3 Program to Check if a string can be obtained by rotating another string 2 places
Given two strings, the task is to find if a string can be obtained by rotating another string two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwise Asked in: Amazon Interview Recommended: Please solve it on “PRACTICE ” f
2 min read
Convert String to Double in Python3
Given a string and our task is to convert it in double. Since double datatype allows a number to have non -integer values. So conversion of string to double is the same as the conversion of string to float This can be implemented in these two ways 1) Using float() method C/C++ Code str1 = "9.02" print("This is the initial string:
2 min read
Python3 Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String
Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; at the end = 0. Sum= 0 + 0 = 0."0011": Count of 0
5 min read
Python3 Program to Minimize characters to be changed to make the left and right rotation of a string same
Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = “abcd”Output: 2Explanation:String after the left shift: “bcda”String after the right shift: “dabc”Changing the character at position 3 to 'a' and c
3 min read
Python3 Program for Queries for rotation and Kth character of the given string in constant time
Given a string str, the task is to perform the following type of queries on the given string: (1, K): Left rotate the string by K characters.(2, K): Print the Kth character of the string. Examples: Input: str = "abcdefgh", q[][] = {{1, 2}, {2, 2}, {1, 4}, {2, 7}} Output: d e Query 1: str = "cdefghab" Query 2: 2nd character is d Query 3: str = "ghab
2 min read
Python3 Program for Swap characters in a String
Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your task is to find the final String after B swaps. Exam
5 min read
Python3 Program for Minimum rotations required to get the same string
Given a string, we need to find the minimum number of rotations required to get the same string.Examples: Input : s = "geeks" Output : 5 Input : s = "aaaa" Output : 1 Method 1: The idea is based on below post.A Program to check if strings are rotations of each other or notStep 1 : Initialize result = 0 (Here result is count of rotations)Step 2 : Ta
2 min read
Python3 Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
3 min read
Practice Tags :