A string is Python’s text type: an immutable sequence of Unicode characters encoded in UTF-8. Words, sentences, paragraphs, and file contents can all be represented and manipulated as strings. Python has no separate character type; a single character is simply a string of length one. section 3.0
Treat strings as sequences rather than only as text. You can iterate over their characters, retrieve elements by index, search with in, and extract subsets with slices. Indexing returns one-character strings, while slicing returns another string. section 3.0 section 4.3
Use these operations to solve common text-processing tasks:
- Search for membership with in, such as word[0] in 'aeiou', instead of repeating a long chain of comparisons. Membership search is linear in the sequence length. section 3.3
- Split text into fields with str.split. Calling split() without an argument treats runs of spaces, tabs, and newlines as one separator; passing a string selects a specific separator. section 3.7
- Reassemble sequence elements with str.join. Use an empty string to join characters without gaps, or a separator such as a space or comma between words. section 3.11 section 3.17
- Sort characters with sorted, then join the resulting list to produce a string in Unicode order. section 3.15
- Remember that strings cannot be changed in place. Rebinding a variable creates a new string reference, while repeated += operations create progressively larger string objects; accumulate pieces in a list and join them instead. section 3.5 section 3.7
Translate a sentence with string operations
A Pig Latin sentence exercise combines splitting, per-word processing, and joining. Split the input into words, translate each word, append the translated results to a list, and join that list with spaces for one-line output. For example, translating "this is a test translation" produces "histay isway away estay ranslationtay". section 3.6 section 3.7
for index, one_letter in enumerate('abcd'):
print(f'{index}: {one_letter}')
Trace enumerate over a string to see how Python supplies each character directly while exposing its numeric index when needed. section 2.15























