Strings
Strings
Introduction
- Strings are a sequence of characters enclosed in quotes.
- Character can be any letter, number, or symbol.
- String characters are accessed via their index, starting at 0.
String Basics
- Strings in Python can be created using either single, double, or triple quotes.
- The len() function returns the length of a string− the count of characters in the string.
- The + operator can concatenate, or join, strings.
- The * operator repeats a string a specified number of times.
Accessing and Slicing Strings
- Indexing can be used to extract single characters from a string.
- The index starts from 0 for the first character.
- Negative indexing can be used to start indexing from the end of the string.
- Strings can be sliced by defining a range of indices. The start index is included but the stop index isn’t.
String Functions
- The strip() function removes whitespace from the beginning or end.
- The lower() and upper() functions respectively convert a string to lower case or upper case.
- The replace() function replaces a specific phrase in a string with another phrase.
- The split() function divides a string into a list of separate strings based on a specified separator.
String Properties
- Strings in Python are immutable, which means once defined, they can’t be changed.
- Attempting to change an indexed position in a string results in a TypeError.
- A new string needs to be created to make any changes or modifications.
Strings enable us to handle and manipulate text-based data in a programming environment. Understanding strings and their operations is crucial for text-based data manipulation and processing.