Sometimes I'm programming and want to know what's in a variable right now. In such a case, I think that I will output it to the console quickly and check it.
For example, like this.
Str name = "Demon Kogure"
int age = 100053
print( name + "Is your age" + age + "I'm old" )
Something like this ... It feels like I'm forcibly sticking a character string, and it's hard to read, isn't it? There are formats and writing methods for variable expansion in various languages, but how do you always write them? While thinking about it, it was awkward to look up, and in the end I repeated things like connecting with +.
So, this time, I investigated how to write variable expansion and format methods in each language. Since the purpose is to output to the console quickly, I will cast and convert the type to String type for the time being.
Java
String name = "Demon Kogure";
int age = 100053;
System.out.println(String.format("%s's age%I'm s years old.", name, age));
//Or (with a newline at the end)
System.out.printf("%s's age%I'm s years old.%n", name, age);
thx: @ saka1029
C#
string name = "Demon Kogure";
int age = 100053;
Console.WriteLine($"{name}Is your age{age}I'm old.");
Python
name = 'Demon Kogure'
age = 100053
print('{}Is your age{}I'm old.'.format(name,age))
#Or
print('%s's age%s years old' % (name,age))
#Or
print(f'{name}Is your age{age}I'm old.')
thx: @QUANON
Ruby
name = "Demon Kogure"
age = 100053
puts "#{name}Is your age#{age}I'm old."
#Or
puts "%s's age%I'm s years old." % [name, age]
thx: @scivola
JavaScript
var _name = "Demon Kogure";
var age = 1000053;
console.log(`${_name}Is your age${age}I'm old.`);
//Or
console.log("%s's age%I'm s years old.", _name, age)
thx: @hogefuga
PHP
$name = "Demon Kogure";
$age = 1000053;
echo "{$name}Is your age{$age}I'm old."
Rust
let name = "Demon Kogure";
let age = 100053;
println!("{}Is your age{}I'm old.", name, age);
thx: @scivola
Kotlin
val name = "Demon Kogure"
val age = 100053
println("${name}Is your age${age}I'm old.")
thx: @sdkei
Now you can output to the console coolly too.
Recommended Posts