Return formatted string in Python

2024/11/18 9:47:56

I have a string:

testString = """ My name is %s
and I am %s years old
and I live in %s"""

I have code that finds these three strings that I want to input into testString. How do I do that? In Java I would've done:

return String.format(testString, string1, string2, string3)

Is there an equivalent in Python?

Answer

this is the version using %:

print """ My name is %s
and I am %s years old
and I live in %s""" % ('tim', 6, 'new york')

and this my preferred version:

testString = """ My name is {}
and I am {} years old
and I live in {}""".format(string1, string2, string3)

you may want to read https://docs.python.org/3.4/library/string.html#string-formatting .

https://en.xdnf.cn/q/118695.html

Related Q&A

How to get the greatest number in a list of numbers using multiprocessing

I have a list of random numbers and I would like to get the greatest number using multiprocessing. This is the code I used to generate the list: import random randomlist = [] for i in range(100000000):…

python pandas yahoo stock data error

i am try to pullout intraday aapl stock data by yahoo. but there problem i facing with my program..import pandas as pd import datetime import urllib2 import matplotlib.pyplot as plt get = http://chart…

Web Scraping Stock Ticker Price from Yahoo Finance using BeautifulSoup

Im trying to scrape Gold stock ticker from Yahoo! Finance. from bs4 import BeautifulSoup import requests, lxmlresponse = requests.get(https://finance.yahoo.com/quote/GC=F?p=GC=F) soup = BeautifulSoup(…

How to convert the radius from meter to pixel?

I have a camera with these specs:full resolution 1280x1024 pixel size 0.0048mm focal length 8 mmI need to detect a ball in this image. It is 4 meters away and its radius is 0.0373 meter. How to convert…

Calculting GPA using While Loop (Python)

A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by…

Return function that modifies the value of the input function

How can I make a function that is given a function as input and returns a function with the value tripled. Here is some pseudo code for what Im looking for. Concrete examples in Python or Scala would b…

how to access the list in different function

I have made a class in which there are 3 functions. def maxvalue def min value def getActionIn the def maxvalue function, I have made a list of actions. I want that list to be accessed in def getaction…

Replacing numpy array with max value [duplicate]

This question already has answers here:numpy max vs amax vs maximum(4 answers)Closed 2 years ago.I have an array, a = np.array([[0,9,8],[5,6,4]])how to replace the each array in axis 1 with the max val…

Finding the max and min in dictionary as tuples python

so given this dictionary im trying to find the max value and min value {Female :[18,36,35,49,19],Male :[23,22,6,36,46]}the output should be in tuples for example key: (min,max)Female: (18,49) Male: (6,…

Flask sqlalchemy relations across multiple files

Im new to Flask Sqlalchemy and I want to declare multiple models and relate them to each other, I followed the example in the documentation but I keep getting this error sqlalchemy.exc.InvalidRequestEr…