Data Types: Strings

In programming, a data type consists of a set of values and a set of operations that can be performed on those values. A literal is the way a value of a data type looks to a programmer. The programmer can use a literal in a program to mention a data value.

Strings

In Python, a string literal is a sequence of characters enclosed in single or double quotation marks. Double-quoted strings are handy for composing strings that contain single quotation marks or apostrophes.

Some examples of string literals include:

  • an empty string- “”
  • spaces- ” “
  • sentences with punctuation- “Here’s some punctuation!”
  • single words- “hello”

Escape sequences

Escape sequences are the way Python expresses special characters, such as the tab, the newline, and the backspace (delete key), as literals.

Some examples of escape sequences include:

  • Backspace- \b
  • Newline- \n
  • Horizontal Tab- \t
  • The \ character- \\
  • Single quotation mark- \’
  • Double quotation mark- \”

String Concatenation

You can join two or more strings to form a new string using the concatenation operator +.

Leave a comment