0

I've been doing recent programs with Java, Python, and C lately while still learning Ruby and Swift, and I have been interested in making print functions in diffrent languages, like Python's print, Java's System.out.println, C's printf and C++'s cout.

I wanna do some "Hello, (user)" program of Python like this:

user = input("What is your name? ")
print(f"Hello, {user}.")

And make a program in Java with the same print function.

public static void main {
    Scanner user;
    print("What is your name? ");
    user = new Scanner(System.in);
    // print("Hello, ", user, ".");
    print(f"Hello, {user}.")
}

I want to use the f format to add in variables in the print function instead of concatenation (the comment in the Java program). I don't know how to do it just yet except the previous form, so I still didn't have tries. Is the f format possible here? Can it only be recreated in a different language? Or do I have to not make the function at all?

  • this may help https://stackoverflow.com/questions/5324007/java-equivalent-of-pythons-format – deadshot Jul 14 '20 at 12:08
  • Does this answer your question? https://stackoverflow.com/questions/6431933/how-to-format-strings-in-java – Joni Jul 14 '20 at 12:10
  • 1
    That f has nothing to do with print in Python. It is one way to create string. Look for "String formatting" or "String interpolation" plus the language on the search engine of your choice. – Niklas Mertsch Jul 14 '20 at 12:26

3 Answers3

1

You do this in python

print("hello {0} and welcome to {1}".format(user, something)) 
# 0 is the index of the variable just like a list

And this is in java

System.out.println(String.format("A String %s %2d", user, intVar); 
// Its print-L-n not print-i-n
// you can use this in python too %s for strings and %d for integers

Python too

print("%s is %d y/o" %(name, age))

%s - String (or any object with a string representation, like numbers), and iterators

%d - Integers

%f - Floating point numbers

%.f - Floating point numbers with a fixed amount of digits to the right of the dot.

%x/%X - Integers in hex representation (lowercase/uppercase)

And the f suffix in python isn't important yet see this if want to know

https://www.python.org/dev/peps/pep-0498/

12ksins
  • 307
  • 1
  • 12
0

Given the Python code

user = input("What is your name? ")
print(f"Hello, {user}.")

the Java equivalent would be

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        System.out.print("What is your name? ");
        String user = scanner.nextLine();
        System.out.printf("Hello, %s.%n", user);
    }
}

To print a formatted string you would use the printf method where you can use a format string as described in the Format string syntax section of the Formatter class.

The so called formatted string literals (f-strings) were only introduced with Python 3.6 (PEP 498), and there's no Java equivalent for them (as of the latest Java 14 release).

(Java also has a MessageFormat which uses a different formatting convention, see also What's the difference between MessageFormat.format and String.format)

Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
0

There's no such way to make the print function in Java, but the equivalent of your code is this:

public static void main(String[] args) {
    System.out.print("What's your name? ");
    Scanner name = new Scanner(System.in);
    System.out.println("Hello, " + name);
}

However, a more possible way to make a print function goes like this:

static void print(String a) {
    System.out.println(a)
}

Yes, there's concatenation instead of f strings, but that's all I can provide.