liveBook · Manning

Empty string

As explained in Python Workout: 50 ten-minute exercises

An empty string is a string containing zero characters.

An empty string is a string containing zero characters. In Python, pressing Enter at an input prompt returns '', not None, and input always returns a string. section 1.2

An empty string is false in an if statement, so Python commonly tests it with if not value rather than comparing explicitly with '' or checking its length. section 1.13 section 4.4

Use empty strings as meaningful control and formatting values:

  • Stop repeatedly prompting when the user submits an empty order; strip whitespace first so space-only input becomes empty. section 4.3 section 4.4
  • Join characters with '' when the output should contain no separators, such as a translated word. section 2.13
  • Pass end='' to print when existing line endings should not receive another newline. section 5.2

Ending a restaurant order

A loop can accept menu items, add their prices, reject unknown keys, and finish when order becomes empty. The idiomatic termination test is if not order; an explicit len(order) == 0 works but is less conventional. section 4.3 section 4.4

if not one_run:
   break

Compare the concise truthiness-based loop exit with the equivalent length-based check. section 1.13 section 4.4

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Defines the value, explains its false conditional behavior, and demonstrates input, looping, joining, and output formatting without covering string internals.
Examples
Demonstrates blank input, restaurant ordering, Ubbi Dubbi output, and printing file lines without doubled newlines.
What it assumes
Assumes basic Python input and strings while teaching that pressing Enter returns an empty string rather than None.
What you can do after
Apply empty-string tests to interactive loops and choose empty-string arguments for controlled joining and printing.
How it connects
Connects empty strings with input, truth values, conditional statements, loops, dictionaries, string joining, and print formatting.

Also explained in

Compare how 3 books explain empty string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout: 50 ten-minute exercises. The section links point to the parts of the book it draws on.