Knowledgebase

Working With Text Print

  • python, errors, troubleshooting, guide, howto, solution, zillionkinghost, hosting
  • 0

Strings in practice.

WHAT A STRING IS

A sequence of characters, in quotes.

COMMON OPERATIONS

Joining Splitting on a separator Changing case Removing surrounding whitespace Replacing part of it Checking whether it contains something

THE WHITESPACE POINT

Text from files and user input frequently carries spaces at the ends.

Stripping it prevents a large class of comparison failures.

FORMATTING VALUES INTO TEXT

Modern Python provides a concise way to insert values into a string.

Use it rather than joining pieces manually.

WHY

It is clearer and less error-prone.

WHAT TO BE CAREFUL WITH

Comparing text with differing case Comparing text with hidden whitespace Assuming a value is text when it is a number

CHARACTERS BEYOND THE BASIC SET

Python 3 handles international characters properly.

Problems usually come from files, not from Python.

WHAT TO DO ABOUT THOSE

Specify the encoding when reading and writing files.

WHAT TO PREFER

Built-in string methods over writing your own.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot