w3resource

Python: Print without newline or space


Print Without Newline

Write a Python program to print without a newline or space.

Sample Solution-1:

Python Code:

# Iterate through a range of numbers from 0 to 9 (inclusive).
for i in range(0, 10):
    # Print an asterisk '*' character on the same line using the 'end' parameter.
    print('*', end="")
# Print a newline character to move to the next line.
print("\n")

Sample Output:

**********

Explanation:

In the above Python code the for loop iterates through the range of numbers from 0 to 9 (inclusive) generated by the range() function. On each iteration, it prints a single asterisk character using the print() function, with the end parameter set to an empty string (""). This causes the print() function not to insert a newline character after each asterisk.

After the loop completes, a newline character is printed to the console using the print() function.

Flowchart:

Flowchart: Print without newline or space.

Sample Solution-2:

Python Code:

# Import the functools module to use the 'partial' function.
import functools

# Create a partial function 'printf' based on the 'print' function with 'end' parameter set to an empty string.
printf = functools.partial(print, end="")

# Iterate through a range of numbers from 0 to 9 (inclusive).
for i in range(0, 10):
    # Use the 'printf' function to print an asterisk '*' character to the same line.
    printf('*')
    
    # Note: The variable 'i' is defined but not used in this code.

Sample Output:

**********

Explanation:

The functools module is for higher-order functions: functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.

functools.partial: Return a new partial object which when called will behave like func called with the positional arguments args and keyword arguments keywords.

In the above Python code we use the ‘functools’ module to create a partial function that prints a string without a newline character.

After defining the printf function, a for loop is used to iterate through the range of numbers from 0 to 9 (inclusive) generated by the range() function. On each iteration, it calls the printf() function to print a single asterisk character to the console without adding a newline character.

Flowchart:

Flowchart: Print without newline or space.

Sample Solution-3:

Python Code:

# Initialize the variable 'i' with the value 0.
i = 0

# Start a while loop that continues as long as 'i' is less than 10.
while i < 10:
    # Print an asterisk '*' to the same line without moving to the next line.
    print('*', end='')

    # Increment the value of 'i' by 1 in each iteration.
    i = i + 1

Sample Output:

**********

Flowchart:

Flowchart: Print without newline or space.

Python Code Editor:

 

Previous: Write a Python program to list all files in a directory in Python.
Next: Write a Python program to determine profiling of Python programs.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.