Nlogk
Code Snippets

String Manipulation

Case Transformation

  • upper(): Converts all characters to uppercase.
  • lower(): Converts all characters to lowercase.
  • title(): Capitalizes the first letter of each word.
  • swapcase(): Swaps the case of each letter (uppercase to lowercase and vice versa).

String Search and Validation

  • startswith(): Checks if the string begins with a specified substring.
  • endswith(): Checks if the string ends with a specified substring.
  • find(): Verifies if a substring exists within the string.
  • count(): Retrieves the index of the first occurrence of a substring.

String Splitting and Joining

  • split(): Divides a string into a list of substrings based on a specified delimiter. In this case, the delimiter is ", ".
  • join(): Combines a list of strings into a single string, inserting a specified separator between each element.
  • splitlines(): Separates a string into a list of lines, using newline characters as the delimiter.

String Cleaning and Formatting

  • strip(): Removes leading and trailing whitespace from the string.
  • lstrip(): Removes whitespace only from the beginning of the string.
  • rstrip(): Removes whitespace only from the end of the string.
  • center(): Centers the string within a specified width, filling the remaining space with a specified character.

String Information

  • isalpha(): checks if all characters in the string are alphabetic (i.e., letters only).
  • isdigit(): checks if all characters in the string are digits..
text = "Hello, World!"
upper_text = text.upper() # "HELLO, WORLD!"
lower_text = text.lower() # "hello, world!"
title_text = text.title() # "Hello, World!"
swapped_case = text.swapcase() # "hELLO, wORLD!"