...

/

Console Input and Output

Console Input and Output

Learn how to get user data from standard inputs and display the results on standard outputs or on our console.

Console input and output

Python and PowerShell are both interactive scripting languages that can capture user data from standard inputs and display the results on standard outputs or on our console. In this lesson, we are going to look into some of the approaches and best practices to read and print to PowerShell and Python consoles.

Printing to console

Python provides a built-in function, print(), which by default prints the value passed to the standard output or any other specified stream.

Press + to interact
# console output
print("Hello World!")
# string with single quotes
print('Hello World in Single quotes')
# \ backslash is the escape char for the single quote
print('Hi, We\'re going to store')
print('Hello','world') # concatenated printing to console
print('Hi'+' There') # concatenated string with + plus sign
print('I am ',28,'years old') # concatenated Numbers and strings
print(19) # print number

This function is just like the Write-Host or Write-Output cmdlet in PowerShell, which are much simpler and we can directly output strings to the console.

There is a catch, however; Write-Host sends output directly to the console.

Press + to interact
# console output
Write-host "Hello World!"
### to the output stream
Write-Output "Hello World!"
### Alternatively
"Hello World!"

Strings inside quotes are sent to a different PowerShell stream called Output stream (the default PowerShell stream). This stream can be piped and used by cmdlets following the pipeline or can be stored in a variable.

Printing without a New-Line

Printing without a new line (essentially, printing without adding a line feed character at the end of the output string) is fairly simple in both PowerShell and Python.

When we don’t want to add a line-feed in PowerShell, we can use the Write-Host cmdlet with a -NoNewLine switch parameter. This will continue printing the item just ...