string manipulation

1

Concatenation: Combining two or more strings.

s1 = "Hello"
s2 = "World"
result = s1 + " " + s2  # Result: "Hello World"

2

Repetition: Repeating a string multiple times.

s = "Hello"
result = s * 3  # Result: "HelloHelloHello"

3

Slicing: Extracting a substring from a string.

s = "Hello World"
substring = s[0:5]  # Result: "Hello"

4

Stripping: Removing leading and trailing whitespace.

s = "   Hello World   "
stripped = s.strip()  # Result: "Hello World"

5

Splitting: Dividing a string into a list of substrings.


6

Joining: Combining a list of strings into a single string.


7

Finding and Replacing: Locating substrings and replacing them.


8

Upper and Lower Case: Converting to uppercase or lowercase.


9

Formatting: Inserting values into a string template.


10

Checking: Testing for specific properties.


11

Capitalizing: Capitalize the first letter of a string.


12

Title Case: Capitalize the first letter of each word.


13

Centering: Center-align a string within a specified width.


14

Justifying Left: Left-align a string within a specified width.


15

Justifying Right: Right-align a string within a specified width.


16

Removing Leading Whitespace: Remove whitespace from the beginning of a string.


17

Removing Trailing Whitespace: Remove whitespace from the end of a string.


18

Counting Substrings: Count occurrences of a substring.


19

Checking Start: Check if a string starts with a specific substring.


20

Checking End: Check if a string ends with a specific substring.


21

Finding Substring: Find the index of the first occurrence of a substring.


22

Finding Substring with a Start Position: Find the index of the first occurrence of a substring starting from a specified position.


23

Finding Substring with a Start and End Position: Find the index of the first occurrence of a substring within a specified range.


24

Replacing with Count: Replace a substring with a new substring a specified number of times.


25

Splitting with a Limit: Split a string into a list with a maximum number of splits.


26

Joining with Separator: Join elements of a list into a single string with a specified separator.


27

Finding All Substrings: Find all occurrences of a substring in a string.


28

Splitting with Regular Expression: Split a string using a regular expression.


29

Substituting with Regular Expression: Replace occurrences of a pattern with a new string using a regular expression.


30

Escaping Special Characters: Escape special characters in a string.


31

Converting to Bytes: Convert a string to bytes.


32

Converting from Bytes: Convert bytes back to a string.


33

Encoding with a Specific Charset: Encode a string using a specific character set.


34

Decoding with a Specific Charset: Decode bytes using a specific character set.


35

Finding the Last Occurrence: Find the index of the last occurrence of a substring.


36

Finding the Last Occurrence with Start Position: Find the index of the last occurrence of a substring starting from a specified position.


37

Reversing a String: Reverse the characters in a string.


38

Checking for Digits: Check if a string consists only of digits.


39

Checking for Alphabetic Characters: Check if a string consists only of alphabetic characters.


40

Checking for Alphanumeric Characters: Check if a string consists only of alphanumeric characters.


41

Checking for Numeric Characters: Check if a string is a numeric string (e.g., '123.45').


42

Checking for Lowercase: Check if all characters in a string are lowercase.


43

Checking for Uppercase: Check if all characters in a string are uppercase.


44

Removing Substring: Remove all occurrences of a substring.


45

Padding with Characters: Pad a string with a specified character to reach a given length.


46

Converting to String: Convert other data types to a string.


47

Formatting Strings with f-strings: Format strings using f-strings (Python 3.6+).


48

Using String Templates: Use the string.Template class for formatting.


49

Formatting Strings with format(): Use the format() method for string formatting.


50

Aligning Strings: Align strings in a given width with padding.


51

Using String Escape Sequences: Include special characters in strings.


52

Converting to Title Case: Convert each word's first letter to uppercase.


53

Trimming Specific Characters: Trim specific characters from the beginning and end of a string.


54

Inserting Substrings: Insert a substring at a specific position.


55

Repeating Substrings: Repeat a substring a specific number of times.


56

Checking for Whitespace: Check if a string contains only whitespace.


57

Padding with a String: Pad a string with another string to a specific length.


58

Removing Trailing Characters: Remove trailing characters.


59

Finding Substring with Regular Expression: Find substrings matching a pattern.


60

Splitting Lines: Split a string into lines.


61

Extracting Substrings with Regular Expressions: Use regex groups to extract parts of a string.


62

Padding with Zeroes: Pad a string with zeroes to ensure a fixed length.


63

Splitting and Extracting by Separator: Split by a separator and extract specific parts.


64

Replacing with Function: Use a function to replace substrings.


65

Using String Interpolation: Format strings with interpolation (Python 2 style).


66

Using format_map() for Dictionary: Format strings using a dictionary.


67

Checking for Printable Characters: Check if all characters are printable.


68

Finding Non-Printable Characters: Check if a string contains non-printable characters.


69

Finding Substring Using index(): Find the index of the first occurrence of a substring (raises an error if not found).


70

Finding Last Occurrence Using rindex(): Find the index of the last occurrence of a substring (raises an error if not found).


71

Replacing Multiple Substrings: Replace multiple substrings using a loop or dictionary.


72

Joining with Multiple Separators: Use multiple separators for joining elements.


73

Getting the ASCII Code: Get the ASCII code of a character.


74

Converting ASCII Code to Character: Convert an ASCII code to a character.


75

Checking for Numeric String with Decimal: Check if a string represents a numeric value with a decimal point.


76

Finding Substrings with Overlapping Matches: Find overlapping occurrences using regex.


77

Using Named Groups in Regex: Extract named groups from a regex match.


78

Checking if String Contains Only Lowercase: Check if all characters are lowercase.


79

Checking if String Contains Only Uppercase: Check if all characters are uppercase.


80

Converting String to List of Characters: Convert a string to a list of its characters.


81

Converting List of Characters to String: Convert a list of characters to a string.


82

Swapping Case: Swap the case of all characters in a string.


83

Using rjust() with Alignment: Right-align and pad with spaces or other characters.


84

Formatting Numbers: Format numbers with specific precision.


85

Detecting Valid Identifiers: Check if a string is a valid Python identifier.


86

Checking for Title Case: Check if a string is in title case.


87

Converting to Raw String: Create a raw string where backslashes are treated literally.


88

Splitting with Multiple Delimiters: Split a string using multiple delimiters with regex.


89

Using lstrip() with Characters: Remove specific characters from the beginning of a string.


90

Using rstrip() with Characters: Remove specific characters from the end of a string.


Last updated