From the course: Python Practice: Operations

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Insert into a string

Solution: Insert into a string - Python Tutorial

From the course: Python Practice: Operations

Solution: Insert into a string

- [Instructor] While it's common to want to modify strings, strings are immutable, so we need to split and reconstruct them into new strings instead of trying to edit them in place. Here, in my solution, first I use string slicing to check characters four through nine of the product ID to see if it already matches the product code we'd like to apply. If it does, we know that this product ID has already had the product code added, so we just return that and the function ends. However, if the product code isn't at positions four through nine of the product ID, we'll need to add it. And that means we'll need to construct a new string with our product code within it. I'll use string slicing to get the first three characters from the string, like the challenge mentions. I store that in a variable I'm calling first part. This gets characters zero, one and two and stops when it reaches three. Then I take the other part of the…

Contents