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.

  1. Python - Create string
  2. Python - Create string using single quotes
  3. Python - Create string using double quotes
  4. Python - Create string using str() builtin function
  5. Python - Create multiline string
  6. Python - Create empty string
  7. Python - Create string of specific length

Create string from other types or values

  1. Python - Create string from list
  2. Python - Create string from list of characters
  3. Python - Create string from tuple
  4. Python - Create string from array
  5. Python - Create string from integer
  6. Python - Create string from variable
  7. 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.

  1. Python - Read string from console
  2. Python - Print string to output

Accessing characters/substrings in String

These tutorials will cover scenarios on how to create strings using different techniques.

  1. Python Substring
  2. Python - Slice a string
  3. Python string - Get character at specific index
  4. Python string - Get first character
  5. Python string - Get last character
  6. Python string - Get first n characters
  7. Python string - Get last n characters
  8. Python - Get substring after specific character
  9. Python - Get substring before specific character
  10. Python - Get substring between two specific characters
  11. Python - Get substring between brackets
  12. Python - Get substring from specific index to end
  13. Python string - Iterate over characters
  14. 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.

  1. Python - Check if string is empty
  2. Python - Check if string contains only alphabets
  3. Python - Check if string contains only alphanumeric
  4. Python - Check if string value is numeric
  5. Python - Check if string contains substring
  6. Python - Check if string contains substring from list
  7. Python - Check if string contains specific character
  8. Python - Check if specific character is present in string
  9. Python - Check if string is a number
  10. Python - Check if string is an integer
  11. Python - Check if string is a float value
  12. Python - Check if all strings in list are not empty
  13. Python - Check if string starts with specific prefix
  14. 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.

  1. Python string - Append
  2. Python string - Append a character to end
  3. Python string - Append new line
  4. Python string - Append number
  5. Python string - Append in loop
  6. Python string - Append variable
  7. Python string - Append to a string variable
  8. Python string - Concatenate
  9. Python - Repeat string N times
  10. Python string - Insert character at start
  11. Python string - Insert character at specific index
  12. 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.

  1. Python - Replace substring
  2. Python string - Replace multiple spaces with single space
  3. Python string - Replace character at specific index
  4. Python string - Replace first occurrence
  5. Python string - Replace last occurrence
  6. Python string - Replace first n occurrences
  7. Python string - Replace all occurrences
  8. Python string - Replace using regular expression
  9. Python string - Replace from dictionary
  10. 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.

  1. Python - Split string
  2. Python - Split string into specific length chunks
  3. Python - Split string by underscore
  4. Python - Split string by space
  5. Python - Split string by new line
  6. Python - Split string by comma
  7. Python - Split string into characters
  8. Python - Split string into N equal parts
  9. Python - Split string into lines
  10. Python - Split string in half
  11. Python - Split string by regular expression

Sorting Strings in Python

  1. Python - Sort list of strings
  2. Python - Sort characters in string

String Transformations in Python

  1. Python - Convert string to lowercase
  2. Python - Convert string to uppercase
  3. Python - Remove white spaces at start and end of string
  4. Python - Capitalise first character of string
  5. Python - Reverse string
  6. Python - Center a string in specific length

Delete Operations in Strings

  1. Python String - Remove character at specific index
  2. Python String - Remove first character
  3. Python String - Remove last character
  4. Python String - Remove substring
  5. Python String - Remove specific character
  6. Python String - Remove first and last character
  7. Python String - Remove first n characters
  8. Python String - Remove last n characters
  9. Python String - Remove first line
  10. Python String - Remove spaces
  11. Python String - Remove special characters

String Conversions

String to other types

  1. Python - Convert string to int
  2. Python - Convert string to float
  3. Python - Convert string to list of characters
  4. Python - Convert string to list
  5. Python - Convert string to dictionary

Other types to string

  1. Python - Convert int to string
  2. Python - Convert float to string
  3. Python - Convert list of characters to string
  4. 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.

  1. Python - Check if two strings are equal
  2. Python - Check if two strings are equal ignore case
  3. Python - Compare strings
  4. Python strings - Compare first n characters
  5. Python strings - Compare first character
  6. Python strings - Compare last character
  7. Python strings - Compare nth character
  8. Python - Find character in first string that are not present in second string
  9. Python - Find characters that are common to given strings

Searching Strings in Python

  1. Python - Find index of substring in a string
  2. Python - Find number of occurrences of a substring in a string
  3. Python - Find number of overlapping occurrences of a substring in a string
  4. Python - Find index of first occurrence of substring in string
  5. Python - Find index of last occurrence of substring in string
  6. Python - Find index of Nth occurrence of substring in string
  7. Python - Find the Longest Common Prefix string

Formatting Strings in Python

  1. Python - Variables in string
  2. Python - Escape single quote inside string
  3. Python - Escape double quotes inside string
  4. Python - Escape backslash inside string
  5. Python - Write string with new line
  6. Python - Print new line after variable

Exceptions and Solutions

  1. Python NameError: name 'string' is not defined

Character related tutorials

  1. Python - Get ASCII value of a character
  2. Python - Check if given character is vowel or consonant
  3. 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.

  1. Python - Print string to console output
  2. Python - String length
  3. Python - List of strings
  4. Python - Print unique characters present in string
  5. Python - Find next character for given character in ASCII
  6. Python - Escape Characters
  7. Python - Get all possible substrings of a string
  8. Python - Get k length substrings of a string



Python Libraries