# [ Python String Methods ] ( CheatSheet )
1. Case Conversion
● lower: [Link]()
● upper: [Link]()
● capitalize: [Link]()
● title: [Link]()
● swapcase: [Link]()
● casefold: [Link]()
2. Checking Content
● isalpha: [Link]()
● isdigit: [Link]()
● isnumeric: [Link]()
● isalnum: [Link]()
● isspace: [Link]()
● istitle: [Link]()
● islower: [Link]()
● isupper: [Link]()
● isdecimal: [Link]()
● isidentifier: [Link]()
● isprintable: [Link]()
3. Searching and Replacing
● startswith: [Link](substring)
● endswith: [Link](substring)
● count: [Link](substring)
● find: [Link](substring)
● index: [Link](substring)
● rfind: [Link](substring)
● rindex: [Link](substring)
● replace: [Link](old, new[, count])
By: Waleed Mousa
4. Character and Substring Manipulation
● strip: [Link]([chars])
● rstrip: [Link]([chars])
● lstrip: [Link]([chars])
● split: [Link]([sep[, maxsplit]])
● rsplit: [Link]([sep[, maxsplit]])
● partition: [Link](sep)
● rpartition: [Link](sep)
● join: [Link](iterable)
● expandtabs: [Link](tabsize)
● center: [Link](width[, fillchar])
● ljust: [Link](width[, fillchar])
● rjust: [Link](width[, fillchar])
● zfill: [Link](width)
5. Text Formatting
● format: [Link](*args, **kwargs)
● format_map: str.format_map(mapping)
● encode: [Link](encoding='utf-8', errors='strict')
● translate: [Link](table)
6. Escape Characters
● Escape Single Quote: 'Don\\'t'
● Escape Double Quote: "He said, \"Hello\""
● Newline: 'Hello\\nWorld'
● Tab: 'Hello\\tWorld'
● Backslash: 'Use \\\\ to represent backslash'
7. Regular Expressions
● [Link]: [Link](pattern, string)
● [Link]: [Link](pattern, string)
● [Link]: [Link](pattern, string)
● [Link]: [Link](pattern, string)
By: Waleed Mousa
● [Link]: [Link](pattern, repl, string)
● [Link]: regex = [Link](pattern)
8. Working with Whitespace
● Remove Leading Whitespace: [Link]()
● Remove Trailing Whitespace: [Link]()
● Remove Both Leading and Trailing Whitespace: [Link]()
● Split Lines: [Link]([keepends])
9. String Testing
● Check for Substring: 'substring' in str
● Check for Absence of Substring: 'substring' not in str
● Check String Equality: str1 == str2
● Check String Inequality: str1 != str2
10. String Information
● Length of String: len(str)
● Minimum Character: min(str)
● Maximum Character: max(str)
11. String Literals
● Raw String: r'raw\string'
● Multiline String: '''Line 1\nLine 2'''
● Concatenation: 'Hello ' + 'World'
● Repetition: 'Repeat ' * 3
12. Advanced String Formatting
● String Interpolation (f-strings): f'Hello, {name}!'
● String Template: from string import Template; t = Template('Hello,
$name!'); [Link](name='World')
13. Unicode Handling
By: Waleed Mousa
● Unicode String: 'unicode string'
● Encode Unicode: 'str'.encode('utf-8')
● Decode Byte to String: b'byte'.decode('utf-8')
14. String Conversion
● String to List: 'str'.split()
● List to String: ''.join(['s', 't', 'r'])
● String to Int: int('42')
● String to Float: float('4.2')
15. Slice and Dice
● Substring Extraction: 'string'[start:end]
● Reverse String: 'string'[::-1]
● Skip Characters while Slicing: 'string'[start:end:step]
16. String Iteration
● Iterate over Characters: [char for char in 'string']
● Enumerate Characters: [(i, char) for i, char in
enumerate('string')]
17. String Comparison
● Lexicographical Comparison: str1 < str2
● Case-Insensitive Comparison: [Link]() == [Link]()
18. String Memory and Identity
● String Identity (is): str1 is str2
● String Identity (is not): str1 is not str2
19. Debugging Strings
● Printable Representation: repr('str\n')
20. String Methods with Keywords
By: Waleed Mousa
● startswith with Tuple of Prefixes: 'string'.startswith(('s', 'st'))
● endswith with Tuple of Suffixes: 'string'.endswith(('g', 'ng'))
21. String Methods and ASCII
● Get ASCII Value of Character: ord('a')
● Get Character from ASCII Value: chr(97)
22. String Constants
● String of ASCII Letters: string.ascii_letters
● String of ASCII Lowercase Letters: string.ascii_lowercase
● String of ASCII Uppercase Letters: string.ascii_uppercase
● String of Digits: [Link]
● String of Hexadecimal Digits: [Link]
● String of Octal Digits: [Link]
● String of Punctuation: [Link]
● String of Printable Characters: [Link]
● String of Whitespace Characters: [Link]
23. String Parsing and Extraction
● Extract Substring by Index: 'string'[1:4]
● Extract Last n Characters: 'string'[-n:]
● Extract First n Characters: 'string'[:n]
24. Checking String Characteristics
● Check if String is All Lowercase: 'string'.islower()
● Check if String is All Uppercase: 'string'.isupper()
● Check if String is Capitalized (First letter uppercase, rest
lowercase): 'string'.istitle()
25. String Mutability
● Immutable Nature of Strings: s = 'string'; s = 'new' + s[5:]
● Creating a New String from the Old One: new_s = s[:5] + 'new' +
s[8:]
By: Waleed Mousa
26. Converting Between Strings and Lists
● Splitting a String into a List of Words: 'The quick brown
fox'.split()
● Joining a List of Words into a String: ' '.join(['The', 'quick',
'brown', 'fox'])
27. Cleaning Strings
● Removing Leading and Trailing Spaces: ' string '.strip()
● Removing Leading Spaces Only: ' string '.lstrip()
● Removing Trailing Spaces Only: ' string '.rstrip()
28. Aligning Strings
● Left Align String: 'string'.ljust(10)
● Right Align String: 'string'.rjust(10)
● Center Align String: 'string'.center(10)
29. String Repetition and Concatenation
● Repeating Strings: 'string' * 3
● Concatenating Strings: 'string1' + 'string2'
30. String Interpolation (Advanced Formatting)
● Old Style (% operator): 'Hello %s' % ('World',)
● New Style (.format): 'Hello {}'.format('World')
31. String Escape Sequences
● New Line: print('line1\\nline2')
● Tab: print('column1\\tcolumn2')
● Backslash: print('Backslash: \\\\')
32. String Literals (Advanced)
● Byte String: b'byte string'
By: Waleed Mousa
● Raw String (suppresses escape sequence processing):
r'raw\string\n'
33. String Unpacking
● Unpacking Characters into Variables: a, b, c = 'abc'
34. Dealing with Quotes in Strings
● Single Quotes Inside Double Quotes: "That's a quote"
● Double Quotes Inside Single Quotes: 'He said, "Hello"'
35. String Documentation (Docstrings)
● Triple-Quoted Multiline Strings as Docstrings: """This is a
docstring"""
36. String Encoding/Decoding
● Encoding a String: 'string'.encode('utf-8')
● Decoding Bytes to String: b'string'.decode('utf-8')
37. String Memory Interning
● Interning Strings: [Link]('string')
38. Making a String "Safe" for Filenames or URLs
● Escape for URL: [Link]('string')
● Safe Filenames: [Link](r'[^\w\s-]', '', 'string').strip().lower()
39. Multi-Line Strings
● Define Multi-Line String: '''Line 1\nLine 2'''
40. Text Wrapping and Filling
● Text Wrapping: [Link]('long string', width=50)
● Text Filling: [Link]('long string', width=50)
By: Waleed Mousa