89DEVs

Python: How to insert character into string?

How to insert a character into a string?

To insert a character into a string, use slice notations and create a new string out of the wanted parts.

option 1: slice notations

To insert a character into a string, use slice notations. # insert character into a string, using slice notations myString = 'Apple is the biggest company' myString = myString[:5] + '™' + myString[5:] print(myString) The character is inserted in the specified position. Apple™ is the biggest company

option 2: print() function

To insert a character into a printed string, use the print function. # insert character into a string, using print() function print('Apple', '™', ' is the biggest company', sep='') The second argument of the print() function specifies the separator between the different parts. Use an empty string to remove spaces. Apple™ is the biggest company

                
        

Summary


Click to jump to section