Python String Manipulation

In a previous section, you learned one or two things about strings of text. You learned that you assign them to your variables the same way you do numbers, only unlike numbers you need some quotation marks:

myText = "String"
myText = 'String'

You can use either double quotes or singles quotes.

Another thing you learned about strings of text is that each character in your string has a position number, called its index:

myText[0]
myText[1]
myText[2]

The index number goes between square brackets.

You also learned that strings are immutable. This means that you can't change them directly with the functions you're about to learn. What the functions do is to return new strings. If all that sounds a little confusing, don't worry: it will be cleared up as you go through the examples.

What you're going to learn in the next few lessons is that there lots of ways you can get at and manipulate strings of text.

 

Changing Case

A simple example of changing strings of text is converting to upper, lower, or proper case. This is easy to do in Python.

Start a new project. Call it anything you like. Now add a Python file to your project. (File > New > Python File. Or right-click the project name in the PyCharm explorer to the left.)

Once you get your blank Python file, enter the following:

myText = "String"

(You can use single quotes instead of the double quotes we're using here. Your choice.)

Now add the following print line:

print( myText.upper() )

The inbuilt Python function is called upper. You type the name of your variable then a dot. After the dot, you type the name of the function you want to use, upper in this case.

After the upper function, you need a pair of round brackets. As its name implies, this converts your string to uppercase letters.

When you try the code out String should turn into STRING.

However, type another print statement just below this one:

print(myText.upper())

print(myText)

What prints out now is this:

STRING

String

As we've said, strings are immutable. The upper function converted the text to uppercase letters. But only temporarily. If you wanted to convert to upper permanently, you need to assign the new letters to a new variable:

myText = "String"
myUpperText = myText.upper()

Change your code to this:

myText = "String"
myUpperText = myText.upper()
print(myText)
print(myUpperText)

You should see String then STRING in the output window.

If you want to convert to all lowercase letters then use the lower() function:

myLowerText = myText.lower()

Give it a try. Add a print line that prints out your myLowerText variable:

myText = "String"
myUpperText = myText.upper()
myLowerText = myText.lower()
print(myText)
print(myUpperText)
print(myLowerText)

If you want proper case (the first letter of each word in capitals) then the function to use is title(). Try this as an example:

myName = "kenny carney"
print(myName.title())

 

In the next lesson, we'll take a look at slicing strings of text in Python.

Python String Slicing >