Copyright © Wei MEI, MLMS™—all rights reserved. 🀤
Python can store and manipulate strings. Strings can be enclosed in single or double quotes. There are a number of string methods you can use to manipulate and work with strings
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes (‘…’) or double quotes (“…”) with the same result  can be used to escape quotes:
>>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'
If you don’t want characters prefaced by to be interpreted as special characters, you can use raw strings by adding an r before the first quote:
>>> print('C:\some\name') # here \n means newline! C:\some ame >>> print(r'C:\some\name') # note the r before the quote C:\some\name
String literals can span multiple lines. One way is using triple-quotes: “””…””” or ‘’’…’’’. End of lines are automatically included in the string, but it’s possible to prevent this by adding a at the end of the line. The following example:
>>> print("""\ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """)
produces the following output (note that the initial newline is not included):
Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to
Strings can be concatenated (glued together) with the + operator, and repeated with *:
>>> >>> # 3 times 'un', followed by 'ium' >>> 3 * 'un' + 'ium' 'unununium'
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
>>> 'Py' 'thon' 'Python'
This feature is particularly useful when you want to break long strings:
>>> text = ('Put several strings within parentheses ' ... 'to have them joined together.') >>> text 'Put several strings within parentheses to have them joined together.'
This only works with two literals though, not with variables or expressions:
>>> prefix = 'Py' >>> prefix 'thon' # can't concatenate a variable and a string literal File "<stdin>", line 1 prefix 'thon' ^ SyntaxError: invalid syntax >>> ('un' * 3) 'ium' File "<stdin>", line 1 ('un' * 3) 'ium' ^ SyntaxError: invalid syntax
If you want to concatenate variables or a variable and a literal, use +:
>>> prefix + 'thon' 'Python'
Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
>>> word = 'Python' >>> word # character in position 0 'P' >>> word # character in position 5 'n'
Indices may also be negative numbers, to start counting from the right:
>>> word[-1] # last character 'n' >>> word[-2] # second-last character 'o' >>> word[-6] 'P'
Note that since -0 is the same as 0, negative indices start from -1.
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:
>>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' >>> word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho'
Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s:
>>> word[:2] + word[2:] 'Python' >>> word[:4] + word[4:] 'Python'
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
>>> word[:2] # character from the beginning to position 2 (excluded) 'Py' >>> word[4:] # characters from position 4 (included) to the end 'on' >>> word[-2:] # characters from the second-last (included) to the end 'on'
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.
For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2.
Attempting to use an index that is too large will result in an error:
>>> word # the word only has 6 characters Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range
However, out of range slice indexes are handled gracefully when used for slicing:
>>> word[4:42] 'on' >>> word[42:] ''
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
>>> word = 'J' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> word[2:] = 'py' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment
If you need a different string, you should create a new one:
>>> 'J' + word[1:] 'Jython' >>> word[:2] + 'py' 'Pypy'
The built-in function len() returns the length of a string:
>>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34
str(object=b'', encoding='utf-8', errors='strict')¶
Return a str version of object. See str() for details.
str is the built-in string class. For general information about strings, see Text Sequence Type — str.
1 2 3 4 5
# You can store strings in variables first_name = 'Susan' # The variable can then be used later in your code print(first_name)
Converting to string values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# There are a number of string functions you can use # on string variables sentence = 'The dog is named Sammy' # upper will return the string in uppercase letters print(sentence.upper()) # lower will return the string in lowercase letters print(sentence.lower()) # capitalize will return the string with the first letter uppercase # and the rest of the string in lowercase print(sentence.capitalize()) # count will count the number of occurrences of the value specified # in the string, in this case how many times the letter 'a' appears print(sentence.count('a'))
When naming variables follow the PEP-8 Style Guide for Python Code
1 2 3 4 5 6 7 8
# Ask the user for their first and last name first_name = input('What is your first name? ') last_name = input('What is your last name? ') # the capitalize function will return the string with # the first letter uppercase and the rest of the word lowercase print ('Hello ' + first_name.capitalize() + ' ' \ + last_name.capitalize())
1 2 3 4 5 6 7 8
# You can use the + operator to concatenate strings first_name = 'Susan' last_name = 'Ibach' print(first_name + last_name) # If you want a space between the strings you must include the space # within the string print('Hello ' + first_name + ' ' + last_name)
Let’s get in and take a look at probably one of the most common things that you’ll be doing in programming, and that is working with strings. Now, when it comes to strings and actually just variables in Python, it’s relatively straightforward to take a string and store it inside of a variable.
Now as a real quick aside, if you’re not already familiar with variables, variables windup acting as placeholders inside of your code for some values.
So let’s take a look at some of those neat little string functions that we saw previously.
So I’m going to start off by just simply typing in first_name equals Christopher, we’ll just skip that stored, and then last_name, equals Harrison.
Perfect. Now, like we have already seen, we can concatenate things together by using that plus sign. So if I say “Print” and I say “first_name,” and by the way, a little IntelliSense Beta functionality for you.
Check the following script and try to find the mistake:
1 2 3 4 5 6
# ask a user to enter their first name and store it in a variable # ask a user to enter their last name and store it in a variable # print their full name # Make sure you have a space between first and last name # Make sure the first letter of first name and last name is uppercase # Make sure the rest of the name is lowercase
1 2 3 4 5 6 7 8 9 10
# ask a user to enter their first name and store it in a variable first_name = input('What is your first name? ') # ask a user to enter their last name and store it in a variable last_name = input('What is your last name? ') # print their full name # Make sure you have a space between first and last name # Make sure the first letter of first name and last name is uppercase # Make sure the rest of the name is lowercase print(first_name.capitalize() + ' ' + last_name.capitalize())
|||Unlike other languages, special characters such as n have the same meaning with both single (‘…’) and double (“…”) quotes. The only difference between the two is that within single quotes you don’t need to escape ” (but you have to escape ‘) and vice versa.|