Python string with parameters

Python String format() Method

Python string format() function has been introduced for handling complex string formatting more efficiently. Sometimes we want to make generalized print statements in that case instead of writing print statement every time we use the concept of formatting.

Python3

This method of the built-in string class provides functionality for complex variable substitutions and value formatting. This new formatting technique is regarded as more elegant. The general syntax of format() method is string.format(var1, var2,…). Here we will try to understand how to Format A String That Contains Curly Braces In Python.

A simple demonstration of Python String format() Method

Formatters work by putting in one or more replacement fields and placeholders defined by a pair of curly braces into a string and calling the str.format(). The value we wish to put into the placeholders and concatenate with the string passed as parameters into the format function.

Syntax of string format() function

Syntax: < >.format(value)

Parameters:

  • value : Can be an integer, floating point numeric constant, string, characters or even variables.

Using a Single Formatter

In this example, we will use the string bracket notation program to demonstrate the str.format() method.

Python3

GeeksforGeeks, A computer science portal for geeks. This article is written in Python Hello, I am 18 years old!

String format() with multiple placeholders

Multiple pairs of curly braces can be used while formatting the string in Python. Let’s say another variable substitution is needed in the sentence, which can be done by adding a second pair of curly braces and passing a second value into the method. Python will replace the placeholders with values in order.

Syntax : < > < >.format(value1, value2)

Parameters : (value1, value2) : Can be integers, floating point numeric constants, strings, characters and even variables. Only difference is, the number of values passed as parameters in format() method must be equal to the number of placeholders created in the string.

Errors and Exceptions :

IndexError : Occurs when string has an extra placeholder, and we didn’t pass any value for it in the format() method. Python usually assigns the placeholders with default index in order like 0, 1, 2, 3…. to access the values passed as parameters. So when it encounters a placeholder whose index doesn’t have any value passed inside as parameter, it throws IndexError.

Python program using multiple placeholders to demonstrate str.format() method.

Python3

GeeksforGeeks, is a computer science portal for geeks Hi! My name is User and I am 19 years old This is one two three four

String format() IndexError

Python program demonstrating Index error number of placeholders is four but there are only three values passed.

Python3

IndexError: tuple index out of range

Formatting Strings using Escape Sequences

You can use two or more specially designated characters within a string to format a string or perform a command. These characters are called escape sequences. An Escape sequence in Python starts with a backslash (\). For example, \n is an escape sequence in which the common meaning of the letter n is literally escaped and given an alternative meaning – a new line.

Escape sequence Description Example
\n Breaks the string into a new line print(‘I designed this rhyme to explain in due time\nAll I know’)
\t Adds a horizontal tab print(‘Time is a \tvaluable thing’)
\\ Prints a backslash print(‘Watch it fly by\\as the pendulum swings’)
\’ Prints a single quote print(‘It doesn\’t even matter how hard you try’)
\” Prints a double quote print(‘It is so\”unreal\”‘)
\a makes a sound like a bell print(‘\a’)

Formatters with Positional and Keyword Arguments

When placeholders are empty, Python will replace the values passed through str.format() in order. The values that exist within the str.format() method are essentially tuple data types and each individual value contained in the tuple can be called by its index number, which starts with the index number 0. These index numbers can be passed into the curly braces that serve as the placeholders in the original string.

  • Positional_argument can be integers, floating point numeric constants, strings, characters and even variables.
  • Keyword_argument is essentially a variable storing some value, which is passed as parameter.

To demonstrate the use of formatters with positional key arguments.

Источник

Читайте также:  Burp suite java version
Оцените статью