Saturday, July 13, 2024

String Literals

 String literals, or “strings,” represent a sequence of characters. Strings are used for various purposes, including displaying screen output, storing text data, and processing textual information.

They can be delimited by either single (') or double (") quotes in Python. Strings must be contained all on one line, except when delimited by triple quotes.


Examples of string literals:

  • ‘Hello’
  • ‘Smith, John’
  • “Baltimore, Maryland 21210”

Strings can contain letters, digits, special characters, and blanks. An empty string is a string consisting of only a pair of matching quotes with nothing in between.

Strings may contain quote characters as long as different quotes are used to delimit the string. For example, “Jennifer Smith’s Friend”.

Python allows using more than one type of quote to handle situations where quotes are part of the string. Conventionally, single quotes are used for delimiting strings, and double quotes are used when needed.