Command Palette

Search for a command to run...

AI Programming

String Methods

Immutable String Nature

Strings in Python have a special property called immutable, meaning that after a string is created, its contents cannot be changed directly. Imagine a string like a book that has already been printed, you cannot change words in it without reprinting the entire page.

When you try to change characters in a string directly, Python will display a TypeError. This is different from lists which can be modified element by element.

Pythonimmutable_string.py
# Demonstration of immutable string natures = "hello"print(f"Initial string: {s}")# Trying to change character directlytry:  s[1] = "a"  # This will cause an errorexcept TypeError as e:  print(f"Error: {e}")# The correct way: creating new strings_new = s[:1] + "a" + s[2:]print(f"New string: {s_new}")# Original string remains unchangedprint(f"Original string still: {s}")

Although strings cannot be changed directly, you can create new strings based on old strings. Operations like concatenation with the + operator will produce new strings, not modify existing ones.

Measuring String Length

The len() function is used to find out the number of characters in a string. Every character, including spaces and punctuation, is counted as one unit.

Pythonstring_length.py
# Measuring length of various string typesstrings = [  "hello",  "Hello World!",  "Python programming",  "",  # empty string  "   ",  # string containing spaces  "123456"]for s in strings:  print(f"'{s}' has length: {len(s)}")# String length with special charactersspecial = "Hello\nWorld\t!"print(f"String with escape sequence: {len(special)} characters")print(f"String display: {repr(special)}")

It's important to understand that escape sequences like \n and \t are counted as one character even though they are written with two symbols.

String Transformation Methods

Changing Letter Case

Python provides several methods to change the letter format in strings. Each method produces a new string with the desired format.

Pythoncase_transformation.py
# Example of case transformation on stringstext = "Python Programming Language"# Convert to all uppercaseupper_text = text.upper()print(f"Upper: {upper_text}")# Convert to all lowercaselower_text = text.lower()print(f"Lower: {lower_text}")# Title case: first letter of each word capitalizedtitle_text = text.title()print(f"Title: {title_text}")# Example with mixed stringmixed = "hELLo WoRLd PyThOn"print(f"Original: {mixed}")print(f"Title case: {mixed.title()}")# Title case on string with numbers and symbolscomplex_text = "python3.9 is-awesome!"print(f"Complex title: {complex_text.title()}")

The title() method is very useful for formatting names or titles, where each word starts with a capital letter.

Removing Characters at Start and End

Strip methods are useful for cleaning strings from unwanted characters at the beginning or end of strings.

Pythonstrip_methods.py
# Demonstration of various strip methodstext = "   Hello World   "print(f"Original: '{text}'")# Remove spaces on both sidesstripped = text.strip()print(f"Strip: '{stripped}'")# Remove spaces only on the leftleft_stripped = text.lstrip()print(f"Left strip: '{left_stripped}'")# Remove spaces only on the rightright_stripped = text.rstrip()print(f"Right strip: '{right_stripped}'")# Strip with special charactersspecial_text = "+++Hello World+++"custom_stripped = special_text.strip("+")print(f"Custom strip: '{custom_stripped}'")# Strip multiple charactersmessy_text = "---+++Hello World+++---"clean_text = messy_text.strip("-+")print(f"Multiple char strip: '{clean_text}'")

Strip methods are very useful in data processing, especially when reading input from users or files that may contain extra spaces.

Search and Validation Methods

Finding Substring Position

The index() and find() methods are used to search for substring positions in strings. The difference lies in how they handle cases when the substring is not found.

Pythonsubstring_search.py
# Substring search with various methodstext = "Python is a powerful programming language"# Using index() - will error if not foundtry:  pos = text.index("powerful")  print(f"'powerful' found at position: {pos}")    # Search for non-existent substring  pos_not_found = text.index("Java")except ValueError as e:  print(f"Error with index(): {e}")# Using find() - returns -1 if not foundpos_find = text.find("powerful")print(f"find('powerful'): {pos_find}")pos_not_found = text.find("Java")print(f"find('Java'): {pos_not_found}")# Search with start and end parameterssubstring = "programming"start_pos = text.find(substring, 20)  # start from position 20print(f"'{substring}' after position 20: {start_pos}")# Count substring occurrencescount = text.count("a")print(f"Letter 'a' appears {count} times")

Use find() when you're not sure if the substring exists in the string, and use index() when you're certain the substring definitely exists.

String Character Validation

Python provides several methods to validate character types in strings.

Pythoncharacter_validation.py
# Example of validating various character typestest_strings = [  "Python",  "Python3",  "12345",  "Hello World",  "hello123",  "UPPERCASE",  "lowercase",  "",  "   "]print("String\t\tAlpha\tDigit\tAlnum\tUpper\tLower")print("-" * 60)for s in test_strings:  alpha = s.isalpha()  digit = s.isdigit()  alnum = s.isalnum()  upper = s.isupper()  lower = s.islower()    print(f"'{s:<10}'\t{alpha}\t{digit}\t{alnum}\t{upper}\t{lower}")# Practical example for input validationdef validate_username(username):  if not username:      return "Username cannot be empty"  if not username.isalnum():      return "Username can only contain letters and numbers"  if len(username) < 3:      return "Username must be at least 3 characters"  return "Username is valid"# Test validationusernames = ["user123", "user@123", "ab", "ValidUser"]for username in usernames:  result = validate_username(username)  print(f"'{username}': {result}")

These validation methods are very useful for verifying user input formats, such as phone numbers, postal codes, or usernames.

Prefix and Suffix Search Methods

The startswith() and endswith() methods allow you to check if a string starts or ends with a specific substring.

Pythonprefix_suffix.py
# Example usage of startswith and endswithfiles = [  "document.pdf",  "image.jpg",  "script.py",  "data.csv",  "README.md",  "backup.zip"]# Check file extensionsprint("Files with specific extensions:")for file in files:  if file.endswith(".py"):      print(f"Python file: {file}")  elif file.endswith((".jpg", ".png", ".gif")):      print(f"Image file: {file}")  elif file.endswith(".pdf"):      print(f"PDF file: {file}")# Check prefixesurls = [  "https://www.google.com",  "http://example.com",  "ftp://files.example.com",  "mailto:user@example.com"]print("\nURL analysis:")for url in urls:  if url.startswith("https://"):      print(f"Secure web: {url}")  elif url.startswith("http://"):      print(f"Web: {url}")  elif url.startswith("ftp://"):      print(f"FTP: {url}")  elif url.startswith("mailto:"):      print(f"Email: {url}")# Using tuple for multiple prefix/suffixtext = "Hello World"print(f"\nStarts with greeting: {text.startswith(('Hi', 'Hello', 'Hey'))}")

Both methods support tuples as parameters, allowing checking of multiple prefixes or suffixes simultaneously.

Replace and Split Methods

Replacing Substrings

The replace() method is used to replace all occurrences of a substring with a new string.

Pythonreplace_method.py
# Example usage of replace methodtext = "Python is great. Python is powerful. Python is easy."print(f"Original: {text}")# Replace all occurrencesreplaced = text.replace("Python", "Java")print(f"Replace all: {replaced}")# Replace with count limitlimited_replace = text.replace("Python", "Java", 2)print(f"Replace 2 first: {limited_replace}")# Replace with empty string (remove)no_dots = text.replace(".", "")print(f"Remove dots: {no_dots}")# Chaining replace for multiple replacementssentence = "I love cats and dogs"animal_replace = sentence.replace("cats", "birds").replace("dogs", "fish")print(f"Chain replace: {animal_replace}")# Case-sensitive replacecase_text = "Hello hello HELLO"case_replace = case_text.replace("hello", "hi")print(f"Case sensitive: {case_replace}")# Practical example: cleaning phone numberphone = "+62-812-3456-7890"clean_phone = phone.replace("-", "").replace("+", "")print(f"Clean phone: {clean_phone}")

The replace() method is case-sensitive and replaces all occurrences unless limited by the third parameter.

Splitting Strings

The split() method breaks a string into a list based on a specified delimiter.

Pythonsplit_method.py
# Various ways to use splittext = "apple,banana,orange,grape"print(f"Original: {text}")# Split with comma delimiterfruits = text.split(",")print(f"Split by comma: {fruits}")# Split without parameter (default whitespace)sentence = "Python is a great language"words = sentence.split()print(f"Split by space: {words}")# Split with limitlimited_split = text.split(",", 2)print(f"Split max 2: {limited_split}")# Split with multiple whitespacemessy_text = "word1    word2\t\tword3\n\nword4"clean_split = messy_text.split()print(f"Clean split: {clean_split}")# Practical example: parsing CSV datacsv_line = "John,25,Engineer,New York"data = csv_line.split(",")name, age, job, city = dataprint(f"Name: {name}, Age: {age}, Job: {job}, City: {city}")# Split with non-existent characterno_delimiter = "no-delimiter-here"result = no_delimiter.split(",")print(f"No delimiter found: {result}")# Joining back with joinjoined = " | ".join(fruits)print(f"Joined with pipe: {joined}")

The split() method is very useful for processing structured data like CSV, parsing user input, or breaking file paths.

String Method Chaining

Since each string method returns a new string, you can combine multiple methods in one expression. This is called method chaining.

Pythonmethod_chaining.py
# Example of method chaining on stringsmessy_input = "   +++ Python Programming for Beginners ---   "print(f"Input: '{messy_input}'")# Chaining multiple methodsclean_result = (messy_input              .strip()  # remove spaces at start/end              .strip("+-")  # remove + and - characters              .lower()  # convert to lowercase              .replace("beginners", "experts"))  # replace wordprint(f"Clean result: '{clean_result}'")# Example chaining for name formattingnames = ["  JOHN DOE  ", "  jane SMITH  ", "  BOB johnson  "]formatted_names = []for name in names:  formatted = name.strip().title()  formatted_names.append(formatted)print("Formatted names:")for name in formatted_names:  print(f"- {name}")# Chaining in list comprehensionsentences = [  "  hello WORLD  ",  "  PYTHON programming  ",  "  DATA science  "]processed = [s.strip().title() for s in sentences]print(f"Processed sentences: {processed}")# Example chaining for validation and formattingdef process_email(email):  return email.strip().lower().replace(" ", "")emails = ["  USER@EXAMPLE.COM  ", "Test User@Gmail.com", "admin@site.org"]for email in emails:  processed = process_email(email)  print(f"'{email}' -> '{processed}'")

Method chaining makes code more concise and readable, especially when you need to perform several transformations consecutively on strings.

Practical String Method Applications

String methods are often used in real programming scenarios to process and validate text data.

Pythonpractical_applications.py
# Practical string method applications in various scenarios# 1. Phone number validation and formattingdef format_phone_number(phone):  # Remove all non-digit characters  digits_only = ""  for char in phone:      if char.isdigit():          digits_only += char    # Format according to Indonesian standard  if len(digits_only) == 13 and digits_only.startswith("62"):      return f"+{digits_only[:2]}-{digits_only[2:5]}-{digits_only[5:9]}-{digits_only[9:]}"  elif len(digits_only) == 12 and digits_only.startswith("08"):      return f"{digits_only[:4]}-{digits_only[4:8]}-{digits_only[8:]}"  else:      return "Invalid number format"phone_numbers = ["081234567890", "+6281234567890", "0812-3456-7890"]for phone in phone_numbers:  formatted = format_phone_number(phone)  print(f"'{phone}' -> '{formatted}'")# 2. Extract information from filenamedef analyze_filename(filename):  # Separate name and extension  if "." in filename:      name, ext = filename.rsplit(".", 1)      ext = ext.lower()  else:      name, ext = filename, ""    # Analyze file type  image_exts = ["jpg", "jpeg", "png", "gif", "bmp"]  doc_exts = ["pdf", "doc", "docx", "txt"]  code_exts = ["py", "js", "html", "css", "java"]    if ext in image_exts:      file_type = "Image"  elif ext in doc_exts:      file_type = "Document"  elif ext in code_exts:      file_type = "Code"  else:      file_type = "Unknown"    return {      "name": name,      "extension": ext,      "type": file_type,      "length": len(filename)  }files = ["photo.jpg", "report.pdf", "script.py", "data", "image.PNG"]for file in files:  info = analyze_filename(file)  print(f"{file}: {info}")# 3. Simple log file parserdef parse_log_entry(log_line):  # Format: [TIMESTAMP] LEVEL: MESSAGE  if not log_line.strip():      return None    # Extract timestamp  if log_line.startswith("[") and "]" in log_line:      end_bracket = log_line.index("]")      timestamp = log_line[1:end_bracket]      rest = log_line[end_bracket + 1:].strip()  else:      timestamp = "Unknown"      rest = log_line.strip()    # Extract level  if ":" in rest:      level, message = rest.split(":", 1)      level = level.strip()      message = message.strip()  else:      level = "INFO"      message = rest    return {      "timestamp": timestamp,      "level": level,      "message": message  }log_entries = [  "[2023-09-17 10:30:15] ERROR: Database connection failed",  "[2023-09-17 10:30:16] INFO: Retrying connection",  "WARNING: Low disk space",  ""]print("\nLog parsing results:")for entry in log_entries:  parsed = parse_log_entry(entry)  if parsed:      print(f"Time: {parsed['timestamp']}, Level: {parsed['level']}, Message: {parsed['message']}")# 4. URL slug generator from titledef create_slug(title):  # Convert to lowercase and replace spaces with dash  slug = title.lower()    # Replace non-alphanumeric characters with dash  clean_slug = ""  for char in slug:      if char.isalnum():          clean_slug += char      elif char == " " or not char.isalnum():          if clean_slug and clean_slug[-1] != "-":              clean_slug += "-"    # Remove dashes at start and end  return clean_slug.strip("-")titles = [  "Learn Python for Beginners",  "Tips & Tricks Programming",  "Data Science: Complete Guide",  "Machine Learning 101"]print("\nSlug generation:")for title in titles:  slug = create_slug(title)  print(f"'{title}' -> '{slug}'")

These four application examples demonstrate the power of combining string methods to solve real-world problems. Method chaining enables complex string transformations in a single line of code, while the immutable nature of strings ensures safe and predictable operations.

By mastering these methods, you can build applications that handle input validation, data parsing, and text transformation efficiently.