A Python class that provides an object-oriented way to walk through strings, get characters at specific positions, and replace characters.
- String Navigation: Walk forward and backward through a string
- Character Access: Get characters at current position or specific positions
- Character Replacement: Replace characters at specific positions or current position
- Position Management: Move to specific positions, start, or end of string
- Bounds Checking: Safe navigation with bounds checking
from string_walker import StringWalker
# Create a StringWalker with a string
walker = StringWalker("Hello, World!")
# Get current position and character
print(walker.get_current_position()) # 0
print(walker.get_current_character()) # 'H'
# Walk forward
walker.walk_forward(3)
print(walker.get_current_character()) # 'l'
# Walk backward
walker.walk_backward(1)
print(walker.get_current_character()) # 'e'
# Get character at specific position
char = walker.get_character_at(7) # 'W'
# Replace character at position
walker.replace_character_at(7, 'X')
print(walker.get_string()) # "Hello, Xorld!"walk_forward(steps=1): Move forward by specified number of stepswalk_backward(steps=1): Move backward by specified number of stepsgo_to_position(position): Move to a specific positiongo_to_start(): Move to the beginning of the stringgo_to_end(): Move to the end of the string
get_current_character(): Get character at current positionget_character_at(position): Get character at specific positionget_current_position(): Get current position
replace_character_at(position, new_char): Replace character at specific positionreplace_current_character(new_char): Replace character at current position
get_string(): Get the current stringget_length(): Get the length of the stringis_at_start(): Check if at the beginningis_at_end(): Check if at the end
To see the StringWalker in action, run:
python3 string_walker.pyThis will execute the demonstration code that shows all the features of the StringWalker class.
The StringWalker is also available as a command-line interface for interactive use:
# Start with an initial string
python3 string_walker_cli.py "Hello World"
# Start without initial string (will prompt for one)
python3 string_walker_cli.py
# Run a quick demo
python3 string_walker_cli.py --demo
# Show help
python3 string_walker_cli.py --helpOnce in the interactive CLI, you can use these commands:
helporh- Show help messagestatusors- Show current statusforward <steps>orf <steps>- Walk forward (default: 1 step)backward <steps>orb <steps>- Walk backward (default: 1 step)goto <position>org <pos>- Go to specific positionstart- Go to start of stringend- Go to end of stringget <position>- Get character at specific positionreplace <pos> <char>- Replace character at positioncurrent <char>- Replace character at current positionstring- Show the current stringlength- Show string lengthclear- Clear the screenquit,q, orexit- Exit the application
walker> f 3 # Walk forward 3 steps
walker> b # Walk backward 1 step
walker> g 5 # Go to position 5
walker> get 7 # Get character at position 7
walker> replace 3 X # Replace character at position 3 with 'X'
walker> current Y # Replace current character with 'Y'
To run comprehensive tests and examples, use:
python3 test_examples.pyThis will run various test scenarios including:
- Basic operations
- Navigation methods
- Character replacement
- Bounds checking
- Edge cases (empty strings, single characters)
The test_examples.py file also includes an interactive demo. To use it, uncomment the last line in the file and run:
python3 test_examples.pyThis will allow you to interactively walk through any string you provide.
=== StringWalker Demo ===
Initial string: 'Hello, World!'
Length: 13
Current position: 0
Current character: 'H'
Walking forward 3 steps...
Position: 3, Character: 'l'
Walking backward 1 step...
Position: 2, Character: 'l'
Getting character at position 7...
Character at position 7: 'W'
Replacing character at position 7 with 'X'...
Updated string: 'Hello, Xorld!'
Going to position 10...
Position: 10, Character: 'd'
Replacing current character with 'Y'...
Updated string: 'Hello, XorlY!'
Going to start...
Position: 0, Character: 'H'
Going to end...
Position: 12, Character: '!'
=== Final State ===
StringWalker at position 12 (char: '!') of 'Hello, XorlY!'
The StringWalker class includes bounds checking:
- Navigation methods return
Trueif successful,Falseif out of bounds - Character access methods return
Noneif the position is out of bounds - Replacement methods return
Falseif the position is out of bounds or the new character is invalid