When working with strings in your software program, you usually:
- Work with the string as a whole: For example, printing someone's name.
- Pick out specific characters within the string: For example, remembering the third character on your to-do list.
In Python, you can process a string using operators with the sequence literal or variable.
Example of Accessing Characters
For example, myname[0] accesses the first letter in a string stored in the variable myname using the variable name and some symbols.
|
An operator is a symbol that performs a specific operation on one or more operands. For instance, the statement employment_income + retirement_income uses the addition (+) operator to add the values of the two variables.
|
Example of Calling Methods
You can also process a string by calling methods on the object holding the sequence. For example, myname.upper() is called on the string object in myname to produce the uppercase version of the string.
|
This works because the Python string type (str) has the upper method defined in its class. You can see more about str by typing help(str) in the Python interactive mode.
Working with String Literals
String literals are the actual string content visible in the source code. This is useful when the string data is assigned initially or used directly.
For example:
|
This code concatenates two string literals using the + operator before printing the result. Python interprets the + operator between two strings as a concatenation operator.
Joining Strings
If you want to include more content than just the existing strings, you can use the join method of the str class. This method allows you to concatenate strings with a fixed string in between each of them.
>>>".".join(["192", "168", "1", "1"]) '192.168.1.1' |
This joins the strings in the list ["192", "168", "1", "1"] with a . in between, resulting in '192.168.1.1'.
Splitting Strings
You can also split a string into separate strings based on a fixed substring.
For example:
>>>'192.168.1.1'.split(".") ['192', '168', '1', '1'] |
This splits the string at each . and results in the list ['192', '168', '1', '1'].
Escape Sequences
Escape sequences are symbols in a string that represent special characters, such as a new line (\n) or a tab (\t). These sequences are used to include special content that isn't visible in the string but needs to be printed.
|
The first statement prints multiple new lines, and the second statement prints a tab space.
Object-Oriented Programming with Strings
In object-oriented programming, an object is like a small machine with valuable data inside. We call methods on the object to make it do useful work with its data. In Python, when you use a string, you're working with a str object, and all the capabilities of the str data type are available for you to use.
This code example demonstrates storing a string in a variable, indexing it to access its parts, and calling a function from its object.
# Store two strings in variables myname = "Monty" mylastname = "Python" # Include the strings in expressions to use their values print("Yeah, I’m " + myname + " " + mylastname + ". Who’s asking?") # Index the strings to access particular parts of them print("Shorten my name to " + myname[0] + mylastname[0] + " if you want, but that’s rude") print("My username? " + myname[0] + mylastname + ". Why are you asking for this??") # join method joins the two strings 'around' the contents of the string object it is called from print("I was told to add strange characters to my password, so I made it " + "✩!@".join([myname, mylastname]) + ". Arggh.. I just said that out loud, didn’t I...") # Call a function from the variable to process it, then print it print(myname.lower()) |
Here’s the output of running the program:
Yeah, I’m Monty Python. Who’s asking? |