More Strings

Searching Strings

To find a particular character in a string, we can use the .find() function.

For example, 'cat'.find('a') takes the string 'cat' and applies the function .find() to it. We specify the character 'a' inside the function (inside the parentheses) to indicate that we are searching for the character 'a'. This command in Python will return the number \(1\) because the character 'a' first appears in the string 'cat' at position \(1\).

'cat'.find('a')
1

Note that .find() will give us only the position of the first instance of a desired character. Suppose that we want to find the position of the character 'o' in the string 'google'. The command 'google.find('o') will return the number \(1\). It will never give us back the number \(2\), even though the character 'o' also appears in that position in the string 'google'.

'google'.find('o')
1
'This is a sentence'.find('is')
2

If a given character is not found in a string, the .find() function will return -1.

'google'.find('a')
-1

Suppose that we are given a tweet and want to check whether the tweet concerns a company of interest. The tweet data will be a string, because tweets are textual data.

tweet = 'Very excited about AAPL earnings next week.'
company = 'AAPL'

if tweet.find(company):
    print('This tweet discusses', company)
else:
    print('Nothing to report.')
This tweet discusses AAPL

Concept check: Find the position of the ticker NVDA in the following string.

string = 'Analysts expect earnings for NVDA to rise over the next four quarters.'

String Concatenation

The way to combine (also known as concatenating) two or more strings is to use the + character. Just like + combines integers and/or floating point numbers, + combines strings.

word1 = 'Apple'
word2 = 'Inc.'
word1 + word2
'AppleInc.'

Notice that + does not join together strings with spaces between them. There are very reasonable applications in which no spacing should occur between strings, and so Python will not assume that you’d like to have spaces between strings.

In the above example, we can safely say that the combined string would look more reasonable with a space between the two words. To achive this, simply include a ' ' string as a middle string in what becomes a concatenation of three strings.

word1 + ' ' + word2
'Apple Inc.'

Note that tricky issues arrive if we use + in circumstances that include both strings and numbers. Obviously, one can’t add strings and numbers together in any sort of algebraic interpretation of the word “add”. Importantly, however, is that Python won’t assume that, in the presence of both strings and numbers, a + character should be interpreted as concatenation. To clarify these last two sentences, let’s attempt two different lines of code.

First, enter a number plus a string.

8 + '1'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-5043558bd679> in <module>
----> 1 8 + '1'

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python prints out an error. The message, in a nutshell, informs us that we can’t use + in the presence of an int and a str variable type.

Second, try things the other way around. Enter a string plus a number.

'1' + 8
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-73173934f639> in <module>
----> 1 '1' + 8

TypeError: can only concatenate str (not "int") to str

Again, Python prints out an error. This time the message is different. It warns us that Python can only concatenate str variables with other str variables (and not int variables.

Why do the messages differ? In the first case, Python sees a number (integer) first. It then assumes that the + character must be addition, and gets confused and raises an error when, after the + character, it encounters a string. In the second case, Python sees a string first. It then assumes that the + character is meant to signify concatenation, and thus becomes confused and raises and error when it sees an integer.

The solution to the problem of combining strings and numbers together is called type conversion, and it will be discussed later in this chapter.

Concept check: Add a new line of code to the end of the block below to combine the variables first and second so that the result is a complete sentence that reads:

Strings are really important pieces of data.

first = 'Strings are really important'
second = 'pieces of data.'