<< Back to F#
F# Printfn Examples: printf, Formats
Write values to the screen with the printfn and printf methods. See format codes for strings and ints.Printfn. A program executes. But it never tells us the values of its variables. Instead we must use printfn to have it report this information. This method is often used.
Format codes. The first argument to printfn or printf must be a format string. The second argument (a value) is inserted in it and written. Printfn has a trailing newline. Printf does not.
First example. This program uses printfn with various format codes. We introduce a mutable id variable. We print it as an integer (numeric) value with the code "%d."
Next: We use the "%A" code, which inserts any value. It does not require a specific type of argument.
String: We can use some surrounding text in our printfn statement. The variable is inserted into this string.
F# program that uses printfn
let mutable id = 10
// Print the variable as an int.
printfn "%d" id
// Print it as a value.
printfn "%A" id
// Use some surrounding text.
printfn "The ID is %A." id
// Modify the id variable and print it again.
id <- 20
printfn "%A" id
Output
10
10
The ID is 10.
20
List, dict. With the "%A" code we can print entire lists or dicts in a single statement. No for-in loop is needed to write all the elements. This makes programs easier to develop.
ListdictF# program that uses printfn with list, dict
let codes = [10; 20; 30]
let data = ["cat", 10; "frog", 40; "rat", 100]
// Use printfn on list and dict types.
// ... All included values are printed to the console.
printfn "%A" codes
printfn "%A" data
Output
[10; 20; 30]
[("cat", 10); ("frog", 40); ("rat", 100)]
Printf, no newlines. This method does not add a trailing newline. So we can use it to write multiple values to a single line in a for-in loop. We print values as strings with the "%s" code.
F# program that uses printf, no newlines
let values = ["parrot"; "finch"; "bluebird"]
// Use printf on values in a list.
// ... No newline is inserted after the values.
for v in values do
printf "%s... " v
Output
parrot... finch... bluebird...
Sprintf. Sometimes we do not want to write to the console. With sprintf we can insert values into a string, and use that elsewhere in the program.
F# program that uses sprintf
// Call sprintf to write values to a string.
let result = sprintf "Result: %A, %A" 10 20
// Now print our string.
printfn "%A" result
Output
"Result: 10, 20"
Parenthesis. When calling a method in a printfn statement, we need to surround it with parentheses. This treats the method as something to be evaluated (a value).
A review. With printfn and printf, we write values to the screen (the console). This helps us develop small and readable F# programs. Format codes help simplify the output.
Related Links:
- F# Files: open System.IO, use StreamReader
- F# Odd, Even Numbers
- F# Record Examples: Type, With Keywords
- F# Recursion Example: rec Keyword
- F# Option int Example: IsNone, IsSome and Value
- F# Array Examples
- F# dict: Dictionary Examples, Item and Keys
- F# IndexOf String Examples: LastIndexOf and Contains
- F# Replace String Examples
- F# Failwith: Exception Handling
- F# Type Example: member, get and set
- F# Match Keyword
- F# Remove Duplicates From List (Seq.distinct)
- F# If, elif and else Examples
- F# Printfn Examples: printf, Formats
- F# Math Operators: abs, floor and ceil
- F# Convert String, Int, Array and Seq
- F# Downcast and upcast Example
- F# ROT13 Cipher (String.map)
- F# String Examples: String.map
- F# Keywords
- F# Seq Examples: Seq.sum, Seq.where
- F# Measure Example: Units of Measurement
- F# Fun Keyword: Lambda Expressions, Functions
- F# Tuple Examples
- F# For and While Loop Examples: For To, For In
- F# Let Keyword: let mutable, Errors
- F# Sort List Examples: sortBy, Lambdas
- F# List Examples, String Lists
- F# Split Strings
Related Links
Adjectives
Ado
Ai
Android
Angular
Antonyms
Apache
Articles
Asp
Autocad
Automata
Aws
Azure
Basic
Binary
Bitcoin
Blockchain
C
Cassandra
Change
Coa
Computer
Control
Cpp
Create
Creating
C-Sharp
Cyber
Daa
Data
Dbms
Deletion
Devops
Difference
Discrete
Es6
Ethical
Examples
Features
Firebase
Flutter
Fs
Git
Go
Hbase
History
Hive
Hiveql
How
Html
Idioms
Insertion
Installing
Ios
Java
Joomla
Js
Kafka
Kali
Laravel
Logical
Machine
Matlab
Matrix
Mongodb
Mysql
One
Opencv
Oracle
Ordering
Os
Pandas
Php
Pig
Pl
Postgresql
Powershell
Prepositions
Program
Python
React
Ruby
Scala
Selecting
Selenium
Sentence
Seo
Sharepoint
Software
Spellings
Spotting
Spring
Sql
Sqlite
Sqoop
Svn
Swift
Synonyms
Talend
Testng
Types
Uml
Unity
Vbnet
Verbal
Webdriver
What
Wpf