String Operators
Python supports fundamental arithmetic-style operators for string manipulation:
- Concatenation (+): Combines two sequences into one.
- Repetition (*): Duplicates a string sequence
ntimes. - Membership (in): Checks if a substring exists within a larger string, returning a boolean.
text_part = "Data Structure"
text_full = text_part + " and Algorithm"
print(text_full)
pattern = "Loop"
cycle = pattern * 2
print(cycle)
if "Struct" in text_full:
print("Substring Found")
else:
print("Not Found")
Built-in String Functions
Several global functions facilitate data inspection and conversion:
Langth Inspection
len(obj) calculates the total count of items. For strings, it counts Unicode code points (characters).
data = "Level 2 Exam Prep"
print(f"Character Count: {len(data)}")
Type Conversion
str(value): Converts input to a string representation.chr(code): Transforms an integer Unicode code point back into a character.ord(char): Retrieves the integer Unicode value of a character.hex(value)/oct(value): Converts integers to hexadecimal or octal string formats.
num_str = str(42)
unicode_char = chr(65)
ascii_val = ord('A')
hex_fmt = hex(255)
print(num_str, unicode_char, ascii_val, hex_fmt)
Standard String Methods
Methods are called on instances using dot notation (object.method()). They primarily return new string objects.
Case Manipulation
.lower(): Returns all characters in lowercase..upper(): Returns all characters in uppercase.
title = "Hello World"
print(title.lower(), title.upper())
Splitting and Joining
.split(sep): Breaks a string into a list based on a delimiter. Default is whitespace..join(iterable): Concatenates elements of a sequence using the string as a separator.
sentence = "Python is powerful"
words = sentence.split()
print(words)
chars = ["P", "y", "t"]
joined_text = ".".join(chars)
print(joined_text)
Counting and Replacement
.count(sub): Tallies occurrences of a specific substring..replace(old, new): Generates a copy where occurrences ofoldare swapped withnew.
test = "banana"
print(test.count("a"))
result = test.replace("a", "o")
print(result)
Alignment and Stripping
.center(width, fill): Pads content to the center..strip(chars): Removes specified characters from both ends (default is whitespace).
mid = "Info".center(10, "*")
clean = " Trim me ".strip()
print(mid, clean)
Lexicographical Comparison
Strings compare sequentially using they underlying Unicode values. Comparison starts from the first character; subsequent characters are only checked if the preceding ones match.
Operators include >, >=, <, <=, ==, !=.
s1 = "Apple"
s2 = "apple"
print(s1 > s2) # True because 'A' < 'a'
print("abc" == "abc")
Type Identification and Casting
Runtime Type Checking
The type(x) function returns the specific class object for any variable. This is useful for conditional logic regarding data types.
val = eval(input("Enter value: "))
if type(val) == int:
print("Integer Input")
elif type(val) == float:
print("Floating Point Input")
else:
print("Other Type")
Numeric Casting
Explicit conversions ensure consistent data handling:
int(x): Truncates decimals for floats; parses numeric strings.float(x): Converts integers or string representations of numbers to floating-point.
n_int = int(3.9)
n_float = float(10)
print(n_int, n_float)
Practical Example: Caesar Cipher
This technique shifts character codes cyclically. It involves mapping ASCII letters by a fixed offset (e.g., +3) while preserving non-alphabetic symbols.
Encryption Logic
The algorithm iterates through input text. If a character is alphabetic, its ordinal value is shifted modulo 26. Chinese characters typically fall within specific Unicode ranges and can be processed similarly.
message = input("Input plaintext: ")
encrypted_buffer = []
for char in message:
code = ord(char)
if "a" <= char <= "z":
# Lowercase shift
offset = (code - ord("a") + 3) % 26
new_char = chr(ord("a") + offset)
encrypted_buffer.append(new_char)
elif "A" <= char <= "Z":
# Uppercase shift
offset = (code - ord("A") + 3) % 26
new_char = chr(ord("A") + offset)
encrypted_buffer.append(new_char)
else:
# Non-letter preservation
encrypted_buffer.append(char)
print("".join(encrypted_buffer))
Decryption Logic
Decryption reverses the shift by subtracting the offset.
secret = input("Input ciphertext: ")
decrypted_buffer = []
for char in secret:
code = ord(char)
if "a" <= char <= "z":
offset = (code - ord("a") - 3) % 26
new_char = chr(ord("a") + offset)
decrypted_buffer.append(new_char)
elif "A" <= char <= "Z":
offset = (code - ord("A") - 3) % 26
new_char = chr(ord("A") + offset)
decrypted_buffer.append(new_char)
else:
decrypted_buffer.append(char)
print("".join(decrypted_buffer))
Extended Support
Handling Unicode beyond standard ASCII requires checking specific hex ranges (e.g., 0x4E00 to 0x9FA5 for common CJK Unified Ideographs).