Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

formatted output

string interpolation
name = "Fufu"
age = 2

print(f"{name} is {age} years old.")
import std/strformat   # &"Hello {name}!"

let
  name = "Fufu"
  age = 2

echo &"{name} is {age} years old."

echo fmt"{name} is {age} years old."    # same thing as &"..."

Output:

Fufu is 2 years old.

&"…" and fmt"…" are very similar but they are not exactly the same. The difference is explained in the docs. I suggest using &"…".

Using a format specifier

format spec
import math

pi = math.pi

print(pi)  # 3.141592653589793

print(f"short: {pi:.2f}")  # short: 3.14
import std/strformat   # &"Hello {name}!"
import std/math        # PI

echo PI  # 3.141592653589793

echo &"short: {PI:.2f}"  # short: 3.14

Using a template string

filling a template string
template = "{0} is {1} years old."

# Fufu is 2 years old.
print(template.format("Fufu", 2))
import std/strutils    # !!! not std/strformat

let templ = "$1 is $2 years old."

# Fufu is 2 years old.
echo templ.format("Fufu", 2)    # new style

# Fufu is 2 years old.
echo templ % ["Fufu", "2"]      # old style; "2" is a string too

Pay attention that template is a keyword in Nim, hence the templ variable name.

The thing ["Fufu", "2"] is an array, and all its elements must have the same type. Here, every element must be a string. Don't forget that the `$` operator that can convert anything to a string.

However, it's simpler to use the .format() "method". There, the arguments can have various types. Another example for this:

import std/strutils

let code = 200

echo "HTTP code: $1".format(code)    # HTTP code: 200

Exercise

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 May 06, 21:35