Python String Operations
String Operations in Python
In most of the applications, data is stored as strings and developers often need to transform the string values using string operations, regular expressions, etc.
These series of tutorials cover examples for how to work with strings in Python language.
String Operations Tutorials
Creating Strings in Python
These tutorials will cover scenarios on how to create strings using different techniques.
- Python - Create string
- Python - Create string using single quotes
- Python - Create string using double quotes
- Python - Create string using str() builtin function
- Python - Create multiline string
- Python - Create empty string
- Python - Create string of specific length
Create string from other types or values
- Python - Create string from list
- Python - Create string from list of characters
- Python - Create string from tuple
- Python - Create string from array
- Python - Create string from integer
- Python - Create string from variable
- Python - Create string from two variables
Read / Print Strings in Python
These tutorials will cover scenarios on how to read string from console, or print string to console.
Accessing characters/substrings in String
These tutorials will cover scenarios on how to create strings using different techniques.
- Python Substring
- Python - Slice a string
- Python string - Get character at specific index
- Python string - Get first character
- Python string - Get last character
- Python string - Get first n characters
- Python string - Get last n characters
- Python - Get substring after specific character
- Python - Get substring before specific character
- Python - Get substring between two specific characters
- Python - Get substring between brackets
- Python - Get substring from specific index to end
- Python string - Iterate over characters
- Python string - Iterate over words
String Checks
These tutorials will cover checking operation with strings like: if the string contains a substring, if string contains any string as substring from a list, if string contains only alphabets, if string is empty, etc.
- Python - Check if string is empty
- Python - Check if string contains only alphabets
- Python - Check if string contains only alphanumeric
- Python - Check if string value is numeric
- Python - Check if string contains substring
- Python - Check if string contains substring from list
- Python - Check if string contains specific character
- Python - Check if specific character is present in string
- Python - Check if string is a number
- Python - Check if string is an integer
- Python - Check if string is a float value
- Python - Check if all strings in list are not empty
- Python - Check if string starts with specific prefix
- Python - Check if string ends with specific suffix
Append / Concatenate / Insert
In the following Python string tutorials, we cover scenarios on how to append a value to a string, or how to concatenate two values into a string, or to insert value inside a string at specific position.
- Python string - Append
- Python string - Append a character to end
- Python string - Append new line
- Python string - Append number
- Python string - Append in loop
- Python string - Append variable
- Python string - Append to a string variable
- Python string - Concatenate
- Python - Repeat string N times
- Python string - Insert character at start
- Python string - Insert character at specific index
- Python - Insert string at specific index
String Replacements in Python
Replacement String Operations are those, in which we replace some characters or substrings with another string. The following tutorials will get you started with some of the replacement operations in Python.
- Python - Replace substring
- Python string - Replace multiple spaces with single space
- Python string - Replace character at specific index
- Python string - Replace first occurrence
- Python string - Replace last occurrence
- Python string - Replace first n occurrences
- Python string - Replace all occurrences
- Python string - Replace using regular expression
- Python string - Replace from dictionary
- Python string - Replace forward slash with backward slash
Splitting string in Python
Splitting is a string operation in which input is a string containing chunks separated by delimiter. We should extract the chunks from the string as a list.
- Python - Split string
- Python - Split string into specific length chunks
- Python - Split string by underscore
- Python - Split string by space
- Python - Split string by new line
- Python - Split string by comma
- Python - Split string into characters
- Python - Split string into N equal parts
- Python - Split string into lines
- Python - Split string in half
- Python - Split string by regular expression
Sorting Strings in Python
String Transformations in Python
- Python - Convert string to lowercase
- Python - Convert string to uppercase
- Python - Remove white spaces at start and end of string
- Python - Capitalise first character of string
- Python - Reverse string
- Python - Center a string in specific length
Delete Operations in Strings
- Python String - Remove character at specific index
- Python String - Remove first character
- Python String - Remove last character
- Python String - Remove substring
- Python String - Remove specific character
- Python String - Remove first and last character
- Python String - Remove first n characters
- Python String - Remove last n characters
- Python String - Remove first line
- Python String - Remove spaces
- Python String - Remove special characters
String Conversions
String to other types
- Python - Convert string to int
- Python - Convert string to float
- Python - Convert string to list of characters
- Python - Convert string to list
- Python - Convert string to dictionary
Other types to string
- Python - Convert int to string
- Python - Convert float to string
- Python - Convert list of characters to string
- Python - Convert bytes to string
Comparing Strings in Python
Comparison is an action to check if two strings are equal, one string is greater than the other, or one string is less than the other.
- Python - Check if two strings are equal
- Python - Check if two strings are equal ignore case
- Python - Compare strings
- Python strings - Compare first n characters
- Python strings - Compare first character
- Python strings - Compare last character
- Python strings - Compare nth character
- Python - Find character in first string that are not present in second string
- Python - Find characters that are common to given strings
Searching Strings in Python
- Python - Find index of substring in a string
- Python - Find number of occurrences of a substring in a string
- Python - Find number of overlapping occurrences of a substring in a string
- Python - Find index of first occurrence of substring in string
- Python - Find index of last occurrence of substring in string
- Python - Find index of Nth occurrence of substring in string
- Python - Find the Longest Common Prefix string
Formatting Strings in Python
- Python - Variables in string
- Python - Escape single quote inside string
- Python - Escape double quotes inside string
- Python - Escape backslash inside string
- Python - Write string with new line
- Python - Print new line after variable
Exceptions and Solutions
Character related tutorials
- Python - Get ASCII value of a character
- Python - Check if given character is vowel or consonant
- Python - Check if a given character is alphabet or not
Other String Tutorials
The following is a list of topics that cover basic string operations in Python.
- Python - Print string to console output
- Python - String length
- Python - List of strings
- Python - Print unique characters present in string
- Python - Find next character for given character in ASCII
- Python - Escape Characters
- Python - Get all possible substrings of a string
- Python - Get k length substrings of a string