This article covers multiple programs in Python, that prints string.
The question is, write a Python program to print a string. The program given below is its answer. This is the simplest program of this article.
print("This is a string.")
The output produced by above Python program is:
This is a string.
This program is created in a way, to initialize the string to a variable first, and put the variable, inside the print() statement, to print the string, hold by the variable.
mystring = "Python Language" print(mystring)
The output will be:
This program receives string from user at run-time of the program, to print the string back on the output screen.
print("Enter the String: ") mystring = input() print("\nYou've entered:", mystring)
The snapshot given below shows the sample run of above Python program, with user input codescracker as string to print it back on the screen:
This program is similar to previous, but the format of the output screen will be different.
print("Enter the String: ", end="") mystring = input() print("\nYou've entered: \"", mystring, "\"", sep="")
The sample run of above Python program, with user input codescracker.com as string, is shown in the snapshot given below:
This is the last program of this article, created to work with multiple strings. That is, this program receives as many strings as user wants, then print all the strings back on the output screen.
print("How many string to enter ? ", end="") noOfString = int(input()) print("Enter", noOfString, "Strings followed by ENTER key: ", end="") mylist = list() for i in range(noOfString): mystring = input() mylist.append(mystring) print("\nStrings entered by you are:") for i in range(noOfString): print("String No.", i+1, ": \"", mylist[i], "\"", sep="")
Here is its sample run with user input 4 as number of string to enter, and codes, cracker, dot, com as four strings:
© Copyright 2021. All Rights Reserved.