Insert Character at Position
Insert character at given position.
IntermediatePhase 5 - Category 4: String TransformationExample 3 of 10
insert-character-at-position.py
1# Take string input2s = input("Enter a string: ")3char = input("Enter character to insert: ")4pos = int(input("Enter position: "))56# Insert7if 0 <= pos <= len(s):8 result = s[:pos] + char + s[pos:]9 print(f"After insertion: {result}")10else:11 print("Invalid position")
Output
Enter a string: Hello Enter character to insert: X Enter position: 2 After insertion: HeXllo
What's going on
Slice string and insert character.
Key Concepts:
s[:pos] is before position
s[pos:] is from position
Insert character between
🔗Related Content
- 📖
Phase 5 - Learn Concepts
Review Phase 5 concepts and explanations
- 📝
Phase 5 - Quiz
Test your Phase 5 understanding with quiz questions
- 💻
Phase 5 - All Practice Problems
Explore all practice problems for Phase 5
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum