Top 50+ Python String Functions

Strings are one of the most fundamental data types in Python, and they come with a wide range of built-in methods for manipulation, formatting, and validation. Whether you need to modify text, search for substrings, format output, or check string properties, Python provides efficient and easy-to-use string functions.

In this article, we will explore all essential string functions in Python, categorized by their use cases. By understanding these functions, you can write cleaner and more efficient code for text processing tasks.


1. String Creation & Checking

Function Description
str() Converts a value into a string.
len(s) Returns the length of the string s.
type(s) Returns the type of s (should be <class 'str'>).
isinstance(s, str) Checks if s is an instance of str.
# 1. str() - Convert any object to string
num = 42
pi = 3.14159
lst = [1, 2, 3]
print(str(num))    # "42"
print(str(pi))     # "3.14159"
print(str(lst))    # "[1, 2, 3]"

# 2. len() - Get string length
text = "Hello World"
print(len(text))   # 11 (spaces count)

# 3. type() - Check variable type
name = "Alice"
print(type(name))  # <class 'str'>

# 4. isinstance() - Verify if object is a string
print(isinstance("Python", str))    # True
print(isinstance(123, str))         # False
print(isinstance([1,2,3], str))     # False

# Practical combined example
def process_input(user_input):
    if not isinstance(user_input, str):
        user_input = str(user_input)
    
    length = len(user_input)
    return f"Processed: {user_input} (Length: {length})"

print(process_input(12345))      # Processed: 12345 (Length: 5)
print(process_input("Hello"))    # Processed: Hello (Length: 5)
print(process_input([1,2,3]))    # Processed: [1, 2, 3] (Length: 9)

# Edge Case Testing
print(str(None))            # "None"
print(len(""))              # 0 (empty string)
print(type(""))             # <class 'str'>
print(isinstance("", str))  # True

Output:

42
3.14159
[1, 2, 3]
11
<class 'str'>
True
False
False
Processed: 12345 (Length: 5)
Processed: Hello (Length: 5)
Processed: [1, 2, 3] (Length: 9)
None
0
<class 'str'>
True

 


2. String Case Conversion

Function Description
s.lower() Converts all characters in s to lowercase.
s.upper() Converts all characters in s to uppercase.
s.capitalize() Capitalizes the first character of s.
s.title() Capitalizes the first letter of each word in s.
s.swapcase() Swaps case: lowercase → uppercase & vice versa.
s.casefold() Converts all characters in s to lowercase, using more aggressive case folding rules than lower() for caseless string matching.
text = "hello world! python is AWESOME 123"

# 1. .lower() - Convert to lowercase
print(text.lower())  
# Output: "hello world! python is awesome 123"

# 2. .upper() - Convert to uppercase
print(text.upper())  
# Output: "HELLO WORLD! PYTHON IS AWESOME 123"

# 3. .capitalize() - Capitalize first character
print(text.capitalize())  
# Output: "Hello world! python is awesome 123"

# 4. .title() - Title case (capitalize each word)
print(text.title())  
# Output: "Hello World! Python Is Awesome 123"

# 5. .swapcase() - Swap cases
print(text.swapcase())  
# Output: "HELLO WORLD! PYTHON IS awesome 123"

# 6. .casefold() - Aggressive lowercase (for case-insensitive comparisons)
print("ß".casefold())  # German sharp s
# Output: "ss"

# Practical Use Cases

# User input normalization
user_input = "  YES  "
if user_input.strip().lower() == "yes":
    print("User agreed!")

# Formatting names
name = "jOHN dOE"
formatted_name = name.title()
print(formatted_name)  # "John Doe"

# Case-insensitive search
products = ["iPhone", "iPad", "MacBook"]
search_term = "ipad"
found = any(search_term.casefold() == p.casefold() for p in products)
print(f"Product found: {found}")  # True

# Edge Cases
empty_string = ""
print(empty_string.upper())  # "" (no error)
numbers = "123"
print(numbers.lower())  # "123" (unchanged)
mixed = "!@#$%"
print(mixed.swapcase())  # "!@#$%" (unchanged)

Output:

hello world! python is awesome 123
HELLO WORLD! PYTHON IS AWESOME 123
Hello world! python is awesome 123
Hello World! Python Is Awesome 123
HELLO WORLD! PYTHON IS awesome 123
ss
User agreed!
John Doe
Product found: True

123
!@#$%

 


3. String Search & Checking

Function Description
s.startswith(substring) Checks if s starts with substring.
s.endswith(substring) Checks if s ends with substring.
s.find(substring) Returns the first index of substring (or -1 if not found).
s.rfind(substring) Returns the last index of substring (or -1 if not found).
s.index(substring) Similar to find(), but raises an error if not found.
s.rindex(substring) Similar to rfind(), but raises an error if not found.
s.count(substring) Returns the number of times substring appears in s.
text = "Hello world! Welcome to Python world. Hello again!"

# 1. startswith() - Check prefix
print(text.startswith("Hello"))      # True
print(text.startswith("world"))      # False
print(text.startswith(("Hello", "Hi")))  # True (tuple of prefixes)

# 2. endswith() - Check suffix
print(text.endswith("again!"))       # True
print(text.endswith("world"))        # False
print(text.endswith((".", "!")))     # True (tuple of suffixes)

# 3. find() - First occurrence index (safe)
print(text.find("world"))           # 6
print(text.find("Python"))          # 21
print(text.find("Java"))            # -1 (not found)

# 4. rfind() - Last occurrence index (safe)
print(text.rfind("world"))          # 28
print(text.rfind("Hello"))          # 34
print(text.rfind("Java"))           # -1 (not found)

# 5. index() - First occurrence (throws error)
print(text.index("world"))          # 6
# print(text.index("Java"))         # ValueError: substring not found

# 6. rindex() - Last occurrence (throws error)
print(text.rindex("world"))         # 28
# print(text.rindex("Java"))        # ValueError: substring not found

# 7. count() - Count occurrences
print(text.count("world"))         # 2
print(text.count("Hello"))         # 2
print(text.count("Java"))          # 0

# Practical Use Cases

# File extension check
filename = "document.pdf"
if filename.endswith((".pdf", ".docx")):
    print("Valid document file")

# Finding domain in email
email = "user@example.com"
at_index = email.find("@")
domain = email[at_index+1:] if at_index != -1 else ""
print(f"Domain: {domain}")  # "example.com"

# Counting vowels
sentence = "The quick brown fox"
vowel_count = sum(sentence.count(v) for v in "aeiouAEIOU")
print(f"Vowel count: {vowel_count}")  # 5

# Edge Cases
empty_str = ""
print(empty_str.find("x"))          # -1
print(empty_str.count("a"))         # 0
# print(empty_str.index("x"))       # ValueError
print("abc".startswith(""))         # True
print("abc".endswith(""))           # True

Output:

True
False
True
True
False
True
6
24
-1
31
38
-1
6
31
2
2
0
Valid document file
Domain: example.com
Vowel count: 5
-1
0
True
True

4. String Modification

Function Description
s.replace(old, new) Replaces occurrences of old with new.
s.strip() Removes leading and trailing spaces from s.
s.lstrip() Removes only leading spaces from s.
s.rstrip() Removes only trailing spaces from s.
s.zfill(width) Pads s with zeros on the left to reach width.
s.center(width, char) Centers s within width using char (default is space).
s.ljust(width, char) Left-aligns s within width using char.
s.rjust(width, char) Right-aligns s within width using char.
s.expandtabs(tabsize) Converts tab characters (\t) to spaces (default: 8 spaces).
# Sample strings for demonstration
original = "   Hello World!   "
filename = "data.txt"
number = "42"
title = "MENU"
code_indent = "def func():\n\tprint('hi')"
whitespace_str = "\t  Hello  \n"

# 1. replace() - Replace substrings
print(original.replace("World", "Python"))  # "   Hello Python!   "
print("banana".replace("a", "o", 2))       # "bonana" (only first 2 replacements)

# 2. strip() - Remove whitespace
print(f"'{original.strip()}'")             # 'Hello World!'
print(f"'{original.lstrip()}'")            # 'Hello World!   '
print(f"'{original.rstrip()}'")            # '   Hello World!'

# 3. zfill() - Zero padding
print(number.zfill(5))                     # "00042"
print("-3.14".zfill(8))                    # "-0003.14"

# 4. center(), ljust(), rjust() - Text alignment
print(title.center(20, "="))               # "========MENU========"
print("Hello".ljust(10, "*"))             # "Hello*****"
print("Hello".rjust(10))                  # "     Hello"

# 5. expandtabs() - Tab conversion
print(code_indent.expandtabs(4))           # "def func():\n    print('hi')"

# Practical Use Cases

# Clean and format filenames
dirty_filename = "  report_final.txt  "
clean_name = dirty_filename.strip().replace(" ", "_")
print(clean_name)                         # "report_final.txt"

# Format currency display
price = "15.5"
print("$" + price.zfill(6))               # "$0015.5"

# Create bordered text
def create_banner(text):
    bordered = f" {text.strip().upper()} "
    return bordered.center(30, "★")
print(create_banner(" special offer "))   # "★★★ SPECIAL OFFER ★★★★"

# Convert tabs to spaces in code
python_code = "if x > 0:\n\treturn True\nelse:\n\treturn False"
print(python_code.expandtabs(2))          # Converts tabs to 2 spaces

# Edge Cases
empty = ""
print(empty.strip())                      # "" (no error)
print(empty.zfill(3))                     # "000"
print(empty.center(5, "*"))               # "*****"

# Special whitespace handling
print(whitespace_str.strip())             # "Hello"
print("hello".replace("", "-"))          # "-h-e-l-l-o-" (inserts between chars)

Output:

   Hello Python!   
bonona
'Hello World!'
'Hello World!   '
'   Hello World!'
00042
-0003.14
========MENU========
Hello*****
     Hello
def func():
    print('hi')
report_final.txt
$0015.5
★★★★★★★ SPECIAL OFFER ★★★★★★★★
if x > 0:
  return True
else:
  return False

000
*****
Hello
-h-e-l-l-o-

 


5. String Splitting & Joining

Function Description
s.split(separator, maxsplit) Splits s into a list using separator.
s.rsplit(separator, maxsplit) Splits s from the right side.
s.splitlines(keepends) Splits s by line breaks (\n, \r\n).
s.partition(substring) Splits s into (before, substring, after).
s.rpartition(substring) Similar to partition(), but searches from the right.
separator.join(iterable) Joins elements of iterable using separator.
# Sample strings for demonstration
sentence = "Hello World,Welcome to Python"
csv_data = "apple,banana,orange,grape"
multiline = "First line\nSecond line\r\nThird line"
path = "/home/user/documents/file.txt"

# 1. split() - Split by separator
print(sentence.split(","))          # ['Hello World', 'Welcome to Python']
print(csv_data.split(",", 2))      # ['apple', 'banana', 'orange,grape'] (max 2 splits)

# 2. rsplit() - Split from right
print(path.rsplit("/", 1))         # ['/home/user/documents', 'file.txt']
print("a.b.c.d".rsplit(".", 2))    # ['a.b', 'c', 'd']

# 3. splitlines() - Split by lines
print(multiline.splitlines())      # ['First line', 'Second line', 'Third line']
print(multiline.splitlines(True))  # Keep line endings: ['First line\n', 'Second line\r\n', 'Third line']

# 4. partition() - Split at first occurrence
print("python_is_awesome".partition("_"))  # ('python', '_', 'is_awesome')
print("noprefix".partition("_"))           # ('noprefix', '', '')

# 5. rpartition() - Split at last occurrence
print("dir/subdir/file.ext".rpartition("/"))  # ('dir/subdir', '/', 'file.ext')
print("no_suffix".rpartition("."))            # ('', '', 'no_suffix')

# 6. join() - Combine iterable with separator
words = ["Hello", "World", "Python"]
print(" ".join(words))             # "Hello World Python"
print("-".join(["2023", "08", "15"]))  # "2023-08-15"

# Practical Use Cases

# Parsing CSV data
csv_row = "John,Doe,30,New York"
first_name, last_name, age, city = csv_row.split(",")
print(f"{first_name} {last_name} is {age} years old")  # "John Doe is 30 years old"

# File extension extraction
full_path = "/home/user/docs/report.pdf"
_, _, filename = full_path.rpartition("/")
name, _, ext = filename.partition(".")
print(f"Extension: {ext}")        # "Extension: pdf"

# Building query strings
params = {"page": "1", "sort": "desc", "filter": "new"}
query = "&".join(f"{k}={v}" for k,v in params.items())
print(query)                     # "page=1&sort=desc&filter=new"

# Processing log files
log_lines = ["[ERROR] 2023-08-15: Disk full",
             "[INFO] 2023-08-15: Backup started"]
for line in log_lines:
    level, _, message = line.partition("] ")
    print(f"{level[1:]}: {message}")

# Edge Cases
empty = ""
print(empty.split(","))           # [''] (not empty list!)
print(empty.splitlines())         # []
print("".join([]))               # "" (empty string)
print("a.b.c".partition("."))    # ('a', '.', 'b.c')
print("a.b.c".rpartition("."))   # ('a.b', '.', 'c')

Output:

['Hello World', 'Welcome to Python']
['apple', 'banana', 'orange,grape']
['/home/user/documents', 'file.txt']
['a.b', 'c', 'd']
['First line', 'Second line', 'Third line']
['First line\n', 'Second line\r\n', 'Third line']
('python', '_', 'is_awesome')
('noprefix', '', '')
('dir/subdir', '/', 'file.ext')
('', '', 'no_suffix')
Hello World Python
2023-08-15
John Doe is 30 years old
Extension: pdf
page=1&sort=desc&filter=new
ERROR: 2023-08-15: Disk full
INFO: 2023-08-15: Backup started
['']
[]

('a', '.', 'b.c')
('a.b', '.', 'c')

 


6. String Type Checking

Function Description
s.isalpha() Returns True if s contains only alphabetic characters.
s.isdigit() Returns True if s contains only digits (0-9).
s.isalnum() Returns True if s contains only letters & digits.
s.islower() Returns True if s is all lowercase.
s.isupper() Returns True if s is all uppercase.
s.isspace() Returns True if s contains only whitespace.
s.istitle() Returns True if s follows title case rules.
s.isidentifier() Returns True if s is a valid Python identifier.
s.isprintable() Returns True if s contains only printable characters.
# Sample strings for demonstration
alpha_str = "Hello"
digit_str = "12345"
alnum_str = "Hello123"
mixed_str = "Hello123!"
whitespace_str = "   \t\n"
title_str = "The Quick Brown Fox"
identifier_str = "valid_variable1"
unicode_str = "こんにちは"  # Japanese 'hello'
control_char = "Hello\x07World"  # Contains bell character

# 1. isalpha() - Alphabetic characters only
print(alpha_str.isalpha())        # True
print(alnum_str.isalpha())        # False (contains numbers)
print(unicode_str.isalpha())      # True (Unicode letters count)

# 2. isdigit() - Digits only
print(digit_str.isdigit())        # True
print("①".isdigit())              # True (Unicode digit)
print("12.3".isdigit())           # False (contains dot)

# 3. isalnum() - Letters and digits only
print(alnum_str.isalnum())        # True
print(mixed_str.isalnum())        # False (contains !)
print("R2D2".isalnum())           # True

# 4. islower() - All lowercase
print("hello".islower())          # True
print("Hello".islower())          # False
print("hello!123".islower())      # True (ignores non-letters)

# 5. isupper() - All uppercase
print("HELLO".isupper())          # True
print("Hello".isupper())          # False
print("HELLO!123".isupper())      # True (ignores non-letters)

# 6. isspace() - Whitespace only
print(whitespace_str.isspace())   # True
print("   x   ".isspace())        # False

# 7. istitle() - Title case format
print(title_str.istitle())        # True
print("The quick Brown".istitle()) # False ('quick' not capitalized)
print("This Is Title!123".istitle()) # True (ignores non-letters)

# 8. isidentifier() - Valid Python identifier
print(identifier_str.isidentifier()) # True
print("2variables".isidentifier())  # False (starts with digit)
print("class".isidentifier())       # True (but reserved keyword)

# 9. isprintable() - Printable characters only
print("Hello".isprintable())       # True
print(control_char.isprintable())  # False (contains \x07)
print("Hello\nWorld".isprintable()) # False (contains newline)

# Practical Use Cases

# Password validation
def is_valid_password(pwd):
    return (len(pwd) >= 8 and 
            any(c.isupper() for c in pwd) and
            any(c.islower() for c in pwd) and
            any(c.isdigit() for c in pwd))
            
print(is_valid_password("Pass1234"))  # True
print(is_valid_password("weak"))      # False

# CSV data validation
def validate_csv_field(field):
    return not any(c.isspace() for c in field.strip()) or field.isnumeric()

print(validate_csv_field(" 123 "))    # True (numeric)
print(validate_csv_field("bad data")) # False (contains spaces)

# Python code analysis
code_snippet = "def calculate_total():"
if code_snippet.split()[0].isidentifier():
    print("Valid function name")

# Edge Cases
print("".isalpha())                  # False (empty string)
print("   ".isalnum())               # False
print("".isprintable())              # True (empty string considered printable)
print("_".isidentifier())            # True (valid in Python)
print("①②③".isdigit())              # True (Unicode numbers)

Output:

True
False
True
True
True
False
True
False
True
True
False
True
True
False
True
True
False
True
False
True
True
False
True
True
False
False
True
False
True
False
Valid function name
False
False
True
True
True

 


7. String Formatting

Function Description
s.format(*args, **kwargs) Formats s using placeholders {}.
s.format_map(mapping) Similar to format(), but uses a dictionary.
f"{variable}" F-strings for inline variable formatting.
"%d %s" % (num, text) Old-style formatting (not recommended).
# 1. format() - Positional and keyword arguments
print("Hello, {}!".format("World"))                  # Hello, World!
print("{1} before {0}".format("apple", "orange"))    # orange before apple
print("Name: {name}, Age: {age}".format(name="Alice", age=30))

# Format specifications
print("Value: {:.2f}".format(3.14159))              # Value: 3.14
print("Hex: 0x{:X}".format(255))                    # Hex: 0xFF
print("Padded: {:>10}".format("test"))              # Padded:       test

# 2. format_map() - Dictionary formatting
user = {"name": "Bob", "score": 95}
print("Player {name} scored {score}%".format_map(user))

# Nested formatting
data = {"user": {"first": "John", "last": "Doe"}}
print("Full name: {user[first]} {user[last]}".format_map(data))

# 3. f-strings (Python 3.6+) - Modern formatting
name = "Alice"
age = 25
print(f"{name} is {age} years old")                 # Alice is 25 years old

# Expressions in f-strings
print(f"Next year: {age + 1}")                      # Next year: 26
print(f"Name uppercase: {name.upper()}")            # Name uppercase: ALICE

# Formatting in f-strings
price = 19.99
print(f"Price: ${price:.2f}")                       # Price: $19.99

# 4. %-formatting - Legacy method (avoid for new code)
print("Name: %s, Age: %d" % ("Charlie", 40))       # Name: Charlie, Age: 40
print("Hex: %x" % 255)                             # Hex: ff

# Practical Use Cases

# Database row formatting
db_row = {"id": 101, "product": "Laptop", "price": 999.99}
print(f"Product {db_row['id']}: {db_row['product']} (${db_row['price']:.2f})")

# Progress reporting
current, total = 25, 100
print(f"Progress: [{current/total:.0%}]")           # Progress: [25%]

# Table formatting
headers = ["Name", "Age", "Score"]
rows = [("Alice", 24, 89.5), ("Bob", 30, 92.3)]
print("{:<10} {:>5} {:>10}".format(*headers))      # Left/right aligned
for row in rows:
    print("{:<10} {:>5} {:>10.1f}".format(*row))

# Localization
locale_data = {"lang": "Python", "year": 1991}
print("The {lang} language was created in {year}".format_map(locale_data))

# Edge Cases
print("{} {}".format(1))                           # Error: Missing argument
print(f"{undefined_var}")                          # Error: Name not defined
print("%(name)s" % {"age": 30})                   # Error: Missing key

Output:

ERROR!
Hello, World!
orange before apple
Name: Alice, Age: 30
Value: 3.14
Hex: 0xFF
Padded:       test
Player Bob scored 95%
Full name: John Doe
Alice is 25 years old
Next year: 26
Name uppercase: ALICE
Price: $19.99
Name: Charlie, Age: 40
Hex: ff
Product 101: Laptop ($999.99)
Progress: [25%]
Name         Age      Score
Alice         24       89.5
Bob           30       92.3
The Python language was created in 1991
Traceback (most recent call last):
  File "<main.py>", line 60, in <module>
IndexError: Replacement index 1 out of range for positional args tuple

 


8. Encoding & Decoding

Function Description
s.encode(encoding, errors) Encodes s into bytes (default: UTF-8).
s.decode(encoding, errors) Decodes bytes to a string.
# Sample strings for demonstration
text = "Hello World! 🌍"
byte_data = b"Hello World!"
special_text = "Café crème 日本"

# 1. encode() - String to bytes
utf8_bytes = text.encode()  # Default UTF-8
print(utf8_bytes)  # b'Hello World! \xf0\x9f\x8c\x8d'

# Different encodings
ascii_bytes = "ABC".encode('ascii')
latin1_bytes = special_text.encode('latin-1', errors='replace')
print(latin1_bytes)  # b'Caf\xe9 cr\xe8me ????'

# 2. decode() - Bytes to string
decoded_text = byte_data.decode('ascii')
print(decoded_text)  # "Hello World!"

# Handling different encodings
jis_bytes = "日本".encode('shift-jis')
decoded_jis = jis_bytes.decode('shift-jis')
print(decoded_jis)  # "日本"

# Practical Use Cases

# Reading/writing files with encoding
with open('example.txt', 'w', encoding='utf-8') as f:
    f.write("Unicode text: ⚡")

with open('example.txt', 'rb') as f:
    content = f.read().decode('utf-8')
    print(content)

# Web requests handling
import requests
response = requests.get('https://example.com')
html_content = response.content.decode('utf-8')

# Database operations
db_blob = b'Database\xc2\xae'  # Example binary data from DB
db_text = db_blob.decode('latin-1')
print(db_text)  # "Database®"

# Error Handling Strategies
text = "Café €100"

try:
    ascii_version = text.encode('ascii')
except UnicodeEncodeError:
    print("Can't encode to ASCII!")

# Using different error handlers
print(text.encode('ascii', errors='ignore'))  # b'Caf 100'
print(text.encode('ascii', errors='replace'))  # b'Caf? ?100'
print(text.encode('ascii', errors='xmlcharrefreplace'))  # b'Caf&#233; &#8364;100'

# Common Encodings
encodings = ['utf-8', 'utf-16', 'ascii', 'latin-1', 'cp1252', 'shift-jis']
for enc in encodings:
    try:
        encoded = text.encode(enc)
        print(f"{enc}: {encoded}")
    except UnicodeError:
        print(f"{enc}: Failed to encode")

# Edge Cases
empty_bytes = b''
print(empty_bytes.decode('utf-8'))  # "" (empty string)

invalid_utf8 = b'\xff\xfe'
print(invalid_utf8.decode('utf-8', errors='replace'))  # "��"

Output:

b'Hello World! \xf0\x9f\x8c\x8d'
b'Caf\xe9 cr\xe8me ??'
Hello World!
日本
ERROR!
Traceback (most recent call last):
  File "<main.py>", line 29, in <module>
PermissionError: [Errno 13] Permission denied: 'example.txt'

 


9. String Translation & Removal

Function Description
s.maketrans(old, new) Creates a translation table for translate().
s.translate(mapping) Translates characters using a mapping.
s.removeprefix(prefix) Removes prefix if it exists in s.
s.removesuffix(suffix) Removes suffix if it exists in s.
# Sample strings for demonstration
text = "Hello World! 123"
filename = "temp_file.txt"
url = "https://example.com/"
secret = "password1234"

# 1. maketrans() + translate() - Character translation
# Create translation table (vowels to numbers)
trans_table = str.maketrans("aeiou", "12345")
print(text.translate(trans_table))  # "H2ll4 W4rld! 123"

# Create deletion table (remove digits)
del_table = str.maketrans("", "", "0123456789")
print(text.translate(del_table))  # "Hello World! "

# 2. removeprefix() - Remove prefix if exists
print(url.removeprefix("https://"))  # "example.com/"
print(filename.removeprefix("temp_"))  # "file.txt"
print(filename.removeprefix("nonexistent_"))  # "temp_file.txt" (no change)

# 3. removesuffix() - Remove suffix if exists
print(filename.removesuffix(".txt"))  # "temp_file"
print(secret.removesuffix("1234"))  # "password"
print(secret.removesuffix("xyz"))  # "password1234" (no change)

# Practical Use Cases

# Data cleaning pipeline
dirty_data = "---CLEAN--THIS--STRING---"
cleaner = str.maketrans("", "", "-")
print(dirty_data.translate(cleaner))  # "CLEANTHISSTRING"

# URL processing
def get_domain(url):
    return (url.removeprefix("http://")
              .removeprefix("https://")
              .removeprefix("www.")
              .split("/")[0])
print(get_domain("https://www.google.com/search"))  # "google.com"

# File extension handling
def change_extension(filename, new_ext):
    return filename.removesuffix(".txt") + new_ext
print(change_extension("data.txt", ".csv"))  # "data.csv"

# Password masking
def mask_secret(secret):
    return secret[:-4].translate(str.maketrans("", "", "0123456789")) + "****"
print(mask_secret("creditCard1234"))  # "creditCard****"

# Edge Cases
empty = ""
print(empty.removeprefix("x"))  # "" (no error)
print(empty.removesuffix("x"))  # "" (no error)

# Unicode translation
greek = "αβγδε"
greek_table = str.maketrans("αβγδε", "abgde")
print(greek.translate(greek_table))  # "abgde"

# Multiple operations
text = "--HELLO--"
print(text.removeprefix("--").removesuffix("--").lower())  # "hello"

Output:

H2ll4 W4rld! 123
Hello World! 
example.com/
file.txt
temp_file.txt
temp_file
password
password1234
CLEANTHISSTRING
google.com
data.csv
creditCard****


abgde
hello

 

Leave a comment