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.
# console outputprint("Hello World!")# string with single quotesprint('Hello World in Single quotes')# \ backslash is the escape char for the single quoteprint('Hi, We\'re going to store')print('Hello','world') # concatenated printing to consoleprint('Hi'+' There') # concatenated string with + plus signprint('I am ',28,'years old') # concatenated Numbers and stringsprint(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.
# console outputWrite-host "Hello World!"### to the output streamWrite-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 ...