From the docs:

  • The format specifiers for general, character, and numeric types have the following syntax:

    %[argument_index$][flags][width][.precision]conversion     
    

The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

String.format("%1s %1s %1s", hello);
Answer from Ignacio Vazquez-Abrams on Stack Overflow
🌐
Savecode
savecode.net › code › java › java+format+use+same+value+multiple+times
java format use same value multiple times - SaveCode.net
April 19, 2021 - String.format("%1$s %1$s %2$s %2$s", "hello", "world"); // hello hello world world
Discussions

java - Can Stringformatter reuse arguments? - Stack Overflow
I'm using String.format to create a formatted string with arguments. Is it somehow possible to tell the formatter to reuse an argument multiple times? More on stackoverflow.com
🌐 stackoverflow.com
java - how to format a string with same value for all placeholders - Stack Overflow
Is there any other way to format ... same value n number of times as parameters ... Sign up to request clarification or add additional context in comments. ... EDIT: It appears that String.format is smarter than I thought. Do the positional syntax given by @Michael · Use MessageFormat. import java.text.Mess... More on stackoverflow.com
🌐 stackoverflow.com
JAVA, interpolate n times the same variable with String.format - Stack Overflow
I would like to interpolate the same variables n times using String.format method. ... You can try using a for-loop as well. ... String value = "hi"; String interpolated = String.format(" %s %s %s %s ", IntStream.range(0, 4) .mapToObj(i -> value) .toArray()); More on stackoverflow.com
🌐 stackoverflow.com
March 3, 2019
How to format multiple string parameters in Java - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
January 25, 2018
🌐
IQCode
iqcode.com › code › java › java-format-use-same-value-multiple-times
java format use same value multiple times Code Example
String.format("%1$s %1$s %2$s %2$s", "hello", "world"); // hello hello world world
Find elsewhere
🌐
javathinking
javathinking.com › blog › java-string-format
Java String.format(): A Comprehensive Guide — javathinking.com
String header = String.format("%-15s | %-10s | %s", "Name", "Age", "City"); String row1 = String.format("%-15s | %-10d | %s", "Alice", 30, "New York"); String row2 = String.format("%-15s | %-10d | %s", "Bob", 25, "London"); System.out.println(header); System.out.println(row1); System.out.println(row2); /* Output: Name | Age | City Alice | 30 | New York Bob | 25 | London */ Argument indexes simplify reusing the same value multiple times (avoids redundant arguments).
🌐
Javaprogramto
javaprogramto.com › 2019 › 03 › java-string-format.html
Java String format() Examples JavaProgramTo.com
November 13, 2021 - ... String format method is used to format the string in the specified format. This method can be used as String output format by passing multiple arguments because the second parameter is the variable-arguments(Var-Args).
🌐
Medium
medium.com › @AlexanderObregon › javas-string-repeat-method-explained-227a93965db2
Java’s String.repeat() Method Explained | Medium
November 24, 2024 - In this article, we’ll break down how the repeat() method works, explore some examples, and take a look at some scenarios where it can be helpful. The repeat() method is a straightforward way to duplicate a string multiple times.
🌐
LabEx
labex.io › tutorials › java-how-to-format-a-string-using-the-string-format-method-in-java-414031
How to format a string using the String.format() method in ...
Learn technical skills with AI and interactive hands-on labs. The AI assistant powered by ChatGPT can help you get unstuck and level up skills quickly while practicing in the in-browser environment.
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-format-method
Java String format() method
June 9, 2024 - There are so many things you can do with this method, for example you can concatenate the strings using this method and, at the same time you can format the output of concatenated string. In this tutorial, we will see several examples of Java String format() method. String formattedString = String.format(String format, Object... args); %s – String · %d – Integer · %f – Floating point number · %t – Date/Time · %x – Hexadecimal · The format specifier %s is replaced by the value of variable name in the output.
🌐
Robert Peng's Blog
mr-dai.github.io › java_string_formatting
Java String Formatting - Robert Peng's Blog
March 25, 2016 - | 159 9F | | < | Formats the same value as the previous secification; for example, %d %<x prints the same number in decimal and hexadecimal. | 159 9F | You can use the static String.format method to create a formatted string without printing it: Conversion characters for Date formatting are also available, which start with t: As you can see in the preceding table, some of the formates yield only a part of a given date – for example, just the day or just the month. It would be a bit silly if you had to supply the day multiple times for format each part.
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › use-string-format-java-string-output
Java String formatting with the String.format method (like ‘sprintf’) | alvinalexander.com
July 30, 2024 - One way to format Java string output is with the format method of the String class, which works like a “Java sprintf” method. Here are some examples: // output a string log.debug( String.format("The rename status is (%d)", RENAME_SUCCEEDED) ); // format some text and assign it to a string ...
🌐
Coderanch
coderanch.com › t › 676428 › java › extract-double-format-String
How to extract double value with the same format from String? (Beginning Java forum at Coderanch)
JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility ... But in both of those cases you almost always decide in advance how many significant digits/digits after the decimal point you plan to show. The idea that you're going to do that based on the input is rather unusual, and I would be interested to see the use case which requires that. ... Hi Sunil, Is this what you are looking for ? String value="0.0001"; double d=(Double.valueOf(value)); System.out.format("%f",d);
🌐
Baeldung
baeldung.com › home › java › java string › java string.format()
Java String.format() | Baeldung
March 23, 2026 - The String.format() method also enables the use of multiple format specifiers in one go, which produces a single string that contains the formatted values:
🌐
Reddit
reddit.com › r/learnpython › how can i string format a single variable (that repeats) at multiple locations in a string?
r/learnpython on Reddit: How can I string format a single variable (that repeats) at multiple locations in a string?
August 25, 2022 -

I am currently learning the string formatting topic and I have searched many places only to get *args and *kwargs (completely unrelated to my question) as my answer. Coming back to my question....

Let's say I have a code to convert decimal numbers to binary, octal and hexadecimal. I wrote the code something like this

num = int(input("Enter an integer: "))

print("Binary of {0} is {1:b}".format(num, num))
print("Hexadecimal of {0} is {1:x}".format(num, num))
print("Octal of {0} is {1:o}".format(num, num))

Here, in the string format method, I am passing 'num' twice to reflect in the printed message. Is there anyway to pass 'num' once to all the placeholders {} in the string?

One more example:

can you can a can as a canner can can a can

For a statement like this, you can code something like below which has repeating "can"s. Instead I want to pass a single string variable for all the placeholders

print("{0} you {1} a {2} as a canner {3} {4} a {5}".format("can", "can", "can", "can", "can", "can"))

Note: Please do suggest a solution to the above, if one such exists. If it doesn't, you can say no. Please please please avoid statements like "You can directly print the string as such instead of going through this ordeal!"