print(), input() and while True: Loops in Python

Опубликовано: 11 Февраль 2026
на канале: Silicon Dojo
304
14

Join the fun: http://www.SiliconDojo.com

Support us with at: http://www.donorbox.com/etcg

We use the print and input functions along with while True loops extensively for example labs. The input function asks for an input from the user and assigns that input to a variable value, print allows you to write either text or a variable value to the screen. And the while True: loop will continuously run a set of code.

Using these three objects together we can create simple labs that allow you to write dynmic scripts that ask for inputs that you can test against. Using a loop is far easier than calling the code to run over and over again.

Print()

The following examples show you how to use the print function. You can print text you manually input, or the value of a variable.

print('Hello World')

or

message = 'Hello Word'

print(message)

Input()

The input() function allows you to take values that the user provides and assign them to a variable.

This example takes your input and assigns it to the text variable and then you print out the value of text.

text = input('Type Something then hit Enter: ')

print(text)

Type Something then hit Enter: hello world!
hello world!

while True:

A while True: loop will create a loop that never ends. We use this in examples as a simple way to continuously loop code so that you can test different inputs.

To exit a while True: loop press control + z , or exit the terminal window.

This example asks for a name and then says Hello NAME.

while True:
name = input('Type a name and hit Enter: ')

print(f'Hello {name}')

Type a nameand hit Enter: bob
Hello bob
Type a nameand hit Enter: tom
Hello tom
Type a nameand hit Enter: sue
Hello sue