Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

Highlights from this chapter include:

  • A string is a sequence of characters.
  • Logical operators can be used to compare two string values. String comparison is done by comparing corresponding ASCII values of characters in the order of appearance in the string.
  • String indexing is used to access a character or a sequence of characters in the string.
  • String objects are immutable.
  • String splicing.

At this point, you should be able to write programs dealing with string values.

Method Description

len()

Returns the string length.

upper()

Returns uppercase characters.

lower()

Returns lowercase characters.

count()

Returns the number of a given substring in a string.

find()

Returns the index of the first occurrence of a given substring in a string. If the substring does not exist in the string, -1 is returned.

index()

Returns the index of the first occurrence of a given substring in a string. If the substring does not exist in the string, a ValueError is returned.

format()

Used to create strings with specified patterns using arguments.

join()

Takes a list of string values and combines string values into one string by placing a given separator between values.

split()

Separates a string into tokens based on a given separator string. If no separator string is provided, blank space characters are used as separators.
Operator Description

in

Checks if a substring exists in a string.

in operator in a for loop

    for character in string:
      # loop body
    
Table 8.4 Chapter 8 reference.
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.