string in python

  • A string is a type of sequence of symbols delimited by a single quote (’), double quotes (”), single triple quotes (”’), or double triple quotes (”””).
  • All the standard sequence operations (indexing, slicing, multiplication, membership, length, minimum, and maximum) work with strings.
  • A string is a sequence of characters. You can access the characters one at a time with the bracket operator.
  • If you are new to Python programming, chances are you won’t need all the options that are available in Python string formatting, so I’ll give you the short version here.
  • >>> fruit = 'banana'
    >>> letter = fruit[1]
  • The second statement extracts the character at index position 1 from the fruit variable and assigns it to the letter variable.
  • The expression in brackets is called an index. The index indicates which character in the sequence you want (hence the name).

String Formatting

A basic conversion specifier consists of the items that follow. Note that the order of these items is crucial.

  • The % character: This marks the beginning of the conversion specifier.
  • Conversion flags: These are optional and may be -, indicating left alignment; +, indicating that a sign should precede the converted value; “ ” (a space character), indicating that a space should precede positive numbers; or 0, indicating that the conversion should be zero-padded.
  • The minimum field width: This is also optional and specifies that the converted string will be at least this wide. If this is an * (asterisk), the width will be read from the value tuple.
  • A . (dot) followed by the precision: This is also optional. If a real number is converted, this many decimals should be shown. If a string is converted, this number is the maximum field width. If this is an * (asterisk), the precision will be read from the value tuple.

String Formatting Conversion Types

    Conversion Type Meaning
    d, i Signed integer decimal
    o Unsigned octal
    u Unsigned decimal
    x Unsigned hexadecimal (lowercase)
    X Unsigned hexadecimal (uppercase)
    e Floating-point exponential format (lowercase)
    E Floating-point exponential format (uppercase)
    f, F Floating-point decimal format
    g Same as e if exponent is greater than –4 or less than precision; f otherwise
    G Same as E if exponent is greater than –4 or less than precision; F otherwise
    c Single character (accepts an integer or a single character string)
    r String (converts any Python object using repr)
    s String (converts any Python object using str)

String methods

  • Strings are an example of Python objects. An object contains both data (the actual string itself) and methods, which are effectively functions that are built into the object and are available to any instance of the object.
  • Python has a function called dir which lists the methods available for an object. The type function shows the type of an object and the dir function shows the available methods.