# Python Programming/Strings

This lesson introduces Python string processing.

## Objectives and Skills

Objectives and skills for this lesson include:
* Standard Library
** String operations

## Readings

* Wikipedia: String (computer science)
* Python for Everyone: Strings

## Multimedia

* YouTube: Python for Informatics - Chapter 6 - Strings
* YouTube: Python - Strings
* YouTube: Python - String Slicing
* YouTube: Python - String Formatting
* YouTube: Python Strings Playlist

## Examples
### len function

The len function returns the length (the number of items) of an object.
Output:

### Strings

Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.
Output:

### Membership Comparisons

The operators in and not in test for membership. x in s evaluates to true if x is a member of s, and false otherwise.
Output:

### String Methods

Strings implement all of the common sequence operations, along with additional methods such as case validation and conversion.
Output:

### String Parsing

Python substrings are referred to as slices. Slices are accessed using the syntax string[start:end], with the first character index starting at zero. The slice will include the characters from start up to but not including end. If end is omitted, the slice will include the characters from start through the end of the string. String slices may use negative indexing, in which case the index counts backwards from the end of the string. The find method returns the lowest index in the string where a substring is found within the given slice. Returns -1 if the substring is not found.
Output:

### String Formatting

String objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values.
Output:

### str.format Method

The str.format method uses format strings that contain “replacement fields” surrounded by curly braces. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.
Output:

### String Interpolation

Introduced in Python 3.6, string interpolation formats strings that contain “replacement fields” surrounded by curly braces, similar to the str.format method. However, an uppercase or lowercase 'f' is placed before the string to indicate string formatting:
Output:

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** LearnPython
*** String Formatting
** TutorialsPoint
*** Strings
** Codecademy
*** Strings and Console Output
** Wikiversity
*** Python/Strings
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Variables, Scripts

### Practice

* Review Python.org: String methods. Create a Python program that asks the user for a line of text containing a first name and last name. Use string methods to parse the line and print out the name in the form last name, first initial, such as Lastname, F. Include a trailing period after the first initial. Ensure that the first letter of each name part is capitalized, and the rest of the last name is lower case. Include error handling in case the user does not enter exactly two name parts. Use a user-defined function for the actual string processing, separate from input and output.
* Review Python.org: String methods. Create a Python program that asks the user for a line of comma-separated-values. It could be a sequence of test scores, names, or any other values. Use string methods to parse the line and print out each item on a separate line. Remove commas and any leading or trailing spaces from each item when printed. If the item is numeric, display it formatted as a floating point value with two decimal places. If the value is not numeric, display it as is.
* Review Python.org: String methods. Create a Python program that asks the user for a line of text that contains HTML tags, such as: <p><strong>This is a bold paragraph.</strong></p> Use string methods to search for and remove all HTML tags, and then print the remaining untagged text. Include error handling in case an HTML tag isn't entered correctly (an unmatched < or >). Use a user-defined function for the actual string processing, separate from input and output.
* Review Python.org: String methods. Create a Python program that asks the user for a line of text. Then ask the user for the number of characters to print in each line, the number of lines to be printed, and a scroll direction, right or left. Using the given line of text, duplicate the text as needed to fill the given number of characters per line. Then print the requested number of lines, shifting the entire line's content by one character, left or right, each time the line is printed. The first or last character will be shifted / appended to the other end of the string. For example: Repeat this. Repeat this.  epeat this. Repeat this. R peat this. Repeat this. Re

## Lesson Summary

* A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.
* A string variable may allow its elements to be mutated and the length changed, or it may be fixed (after creation).
* Python strings are immutable — they cannot be changed.
* The len function returns the length (the number of items) of an object.
* Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.
* The operators in and not in test for membership. x in s evaluates to true if x is a member of s, and false otherwise.
* String methods include: isalpha, islower, isnumeric, isspace, istitle, isupper, lower, strip, swapcase, title, and upper.
* Python substrings are referred to as slices. Slices are accessed using the syntax string[start:end], with the first character index starting at zero. The slice will include the characters from start up to but not including end. If end is omitted, the slice will include the characters from start through the end of the string.
* String slices may use negative indexing, in which case the index counts backwards from the end of the string.
* The find method returns the lowest index in the string where a substring is found within the given slice. Returns -1 if the substring is not found.
* String objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values.
* The str.format method uses format strings that contain “replacement fields” surrounded by curly braces. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.

## Key Terms
### counter

    A variable used to count something, usually initialized to zero and then incremented.

### empty string

    A string with no characters and length 0, represented by two quotation marks.

### format operator

    An operator, %, that takes a format string and a tuple and generates a string that includes the elements of the tuple formatted as specified by the format string.

### format sequence

    A sequence of characters in a format string, like %d, that specifies how a value should be formatted.

### format string

    A string, used with the format operator, that contains format sequences.

### flag

    A boolean variable used to indicate whether a condition is true.

### invocation

    A statement that calls a method.

### immutable

    The property of a sequence whose items cannot be assigned.

### index

    An integer value used to select an item in a sequence, such as a character in a string.

### item

    One of the values in a sequence.

### method

    A function that is associated with an object and called using dot notation.

### object

    Something a variable can refer to. For now, you can use “object” and “value” interchangeably.

### search

    A pattern of traversal that stops when it finds what it is looking for.

### sequence

    An ordered set; that is, a set of values where each value is identified by an integer index.

### slice

    A part of a string specified by a range of indices.

### traverse

    To iterate through the items in a sequence, performing a similar operation on each.

## Review Questions

* A string is _____. A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.
* A string variable may _____. A string variable may allow its elements to be mutated and the length changed, or it may be fixed (after creation).
* Python strings are _____ — they cannot be changed. Python strings are immutable — they cannot be changed.
* The len function returns _____. The len function returns the length (the number of items) of an object.
* Textual data in Python is handled with _____. Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points.
* The operators _____ and _____ test for membership. x _____ s evaluates to true if x is a member of s, and false otherwise. The operators in and not in test for membership. x in s evaluates to true if x is a member of s, and false otherwise.
* String methods include: String methods include: isalpha, islower, isnumeric, isspace, istitle, isupper, lower, strip, swapcase, title, and upper.
* Python substrings are referred to as _____. Python substrings are referred to as slices.
* Slices are accessed using the syntax _____. Slices are accessed using the syntax string[start:end].
* The first character index in a slice starts at _____. The slice will include _____. If end is omitted, the slice will include _____. The first character index in a slice starts at zero. The slice will include the characters from start up to but not including end. If end is omitted, the slice will include the characters from start through the end of the string.
* String slices may use negative indexing, in which case _____. String slices may use negative indexing, in which case the index counts backwards from the end of the string.
* The find method returns _____. The find method returns the lowest index in the string where a substring is found within the given slice, and returns -1 if the substring is not found.
* String objects have one unique built-in operation: the % operator (modulo). This is also known as _____. String objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator. Given format % values (where format is a string), % conversion specifications in format are replaced with zero or more elements of values.
* The str.format method uses format strings that contain “replacement fields” surrounded by _____. The str.format method uses format strings that contain “replacement fields” surrounded by curly braces. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.

## Assessments

* Flashcards: Quizlet: Python Strings
* Quiz: Quizlet: Python Strings

## See Also

* Python.org: An Informal Introduction to Python
* PythonBasics: Strings and exercises
* SoloLearn: Python
* Open Book Project: Python 3 - Strings
* Google Developers: Python Strings
* Learn Python: Basic String Operations
* PythonForBeginners: String Manipulation
* W3Schools: Python String Methods
* Real Python: Python String Formatting