How to take user input in Julia?
Users can interact with the program and provide dynamic values to the variables with the help of inputs. In this answer, we'll learn to take input from the user in Julia.
Julia provides below two methods to take user input from the console.
readline(): This method will read one line of user input as a string from the console.STDIN Standard input and output readlines(): This method will read the multiline of user input as a vector of strings from the console.STDIN Standard input and output
Syntax
# syntax for single line input from the userreadline()# syntax for multiline input from the userreadlines()
Example for a single line input
println("Enter your name: ")#Take single line user input from the useruser_name = readline()println("Hello $user_name from Educative π")
Enter the input below
Code explanation
Line 4: We take the user name as input from the STDIN console using the I/O method
readline(), and assign value to a variableuser_name.Line 6: We print a message including the user name input value
user_name.
Example for multiple lines input
println("Enter your toppings: ")#Take multiline input from the usertoppings = readlines()println("You have provided below toppings.")println(toppings)
Enter the input below
Code explanation
Line 4: We take the toppings as input from the STDIN console using the I/O method
readlines(), and assign value to a variabletoppings.Line 7: We display the toppings,
toppings.
Free Resources