Python Basics 5 - Strings

Python Basics 5 - Strings

What are Python strings?

String is an object that consist of letters, special characters, spaces, digits enclosed in double quotation marks ("...") or single quotes ('...'). In Python, strings are immutable, which means that once you create a string, you cannot change it.

Here's an example:

my_string = "Hello, World!"

We prefer to read data in using strings and then parse and convert data as we need. This gives us more control over error situations and/or bad user input. Input numbers must be converted from strings.

>>> x=input("enter:")
enter:100
>>> y=int(x)-10
90

Looking Inside Strings

>> fruit="banana"
>>>letter=fruit[1]
>>>print(letter)
a
>>>x=3
>>>w=fruit[x-1]
>>>print(w)
n

We can get at any single character in a string using an index specified in square brackets just like above program. The index indicates which character in the sequence you want (hence the name). The index value must be an integer. The index value can be an expression that is computed.

You might have notice in above program that fruit[1] returns a instead of b . For most people, the first letter of “banana” is “b”, not “a”. But in Python, the index is an offset from the beginning of the string, and the offset of the first letter is zero.


b  a  n  a  n  a
0  1  2  3  4  5

So “b” is the 0th letter (“zero-th”) of “banana”, “a” is the 1th letter (“one-th”), and “n” is the 2th (“two-th”) letter and so on.

You will get a python error if you attempt beyond the end of string.

>>> fruit = "banana"
>>> print(fruit[6])

Error: string index out of range

Negative numbers are also used to index a string. -1 represents the last letter of string while

-len(string) represents the first letter.


 b   a   n   a   n   a
-6  -5  -4  -3  -2  -1
>>> fruit = "banana"
>>> print(fruit[-1])
a
>>>print(fruit[-len(string)])
b

String Operations and Manipulation

Concatenation of Strings

>>> hi="hello there"
>>> name="aayush"
>>> greet=hi + " " + name
>>> print(greet)
hello there aayush

The + is said to be overloaded i.e, has different meanings depending upon the types of objects to which it is applied. For int it means addition while in string it means concatenation.

Successive Concatenation :

>>> name = "aayush"
>>> 3*name
"aayushaayushaayush"

Here * operator means repetition in string.

Finding length of a string

The built in function len gives us the length of string.

>>> fruit="apple"
>>> print(len(fruit))
5

String slices

A segment of a string is called a slice. Selecting a slice is similar to selecting a character:

>>> s = 'The Pythonic Way'
>>> print(s[0:3])
The
>>> print(s[4:12])
Pythonic
print(s[13:16])
Way

The operator [n:m] returns the part of the string from the “n-th” character to the “m-th” character, including the first but excluding the last.

If you omit the first index (before the colon), the slice starts at the beginning of the string. If you omit the second index, the slice goes to the end of the string:

>>> fruit = 'banana'
>>> fruit[:3]
'ban'
>>> fruit[3:]
'ana'

If the first index is greater than or equal to the second the result is an empty string, represented by two quotation marks:

>>> fruit = 'banana'
>>> fruit[3:3]
''

An empty string contains no characters and has length 0, but other than that, it is the same as any other string.

Adding a third parameter, k like this: s[i:j:k]. This gives a slice of the string s from index to index j-1, with step size k.


>>> s='Introduction to Computer Science'
>>> len(s)
32
>>> s[1:30:2]
'nrdcint optrSin'
>>> s[::2]
'Itouto oCmue cec'

Last example s[::2] shows full string s from index 0 through 31), with a step size of 2 so we end up with every other character in string s

We can even reverse the string using [::-1]

>>> s = "apple"
>>> s[::-1]
"elppa"

The in operator

The word in is a boolean operator that takes two strings and returns True if the first appears as a substring in the second:

>>> 'a' in 'apple'
True
>>> 'doctor' in 'apple'
False

String comparison

The comparison operators work on strings. To see if two strings are equal:

if word == 'banana':
    print('All right, bananas.')

Other comparison operations are useful for putting words in alphabetical order:

if word < 'banana':
    print('Your word,' + word + ', comes before banana.')
elif word > 'banana':
    print('Your word,' + word + ', comes after banana.')
else:
    print('All right, bananas.')

Python does not handle uppercase and lowercase letters the same way that people do. All the uppercase letters come before all the lowercase letters, so:

Your word, Cat, comes before banana.

A common way to address this problem is to convert strings to a standard format, such as all lowercase, before performing the comparison.

String Methods

Strings are an example of Python objects. An object contains both data (the actual string itself) and methods, which are effectively functions that are built into the object and are available to any instance of the object.

For example, the method upper takes a string and returns a new string with all uppercase letters:

>>> word = 'cat'
>>> new_word = word.upper()
>>> print(new_word)
CAT

There is a string method named find that searches for the position of one string within another:

>>> word = 'Aaple'
>>> index = word.find('a')
>>> print(index)
1

Sometimes we want to take a string and remove whitespace at the beginning and/ or end. lstrip() and rstrip() remove whitespace at the left or right respectively. strip() removes both beginning and ending whitespaces

>>> line = '  Here we go  '
>>> line.strip()
'Here we go'

There are numerous other string method. You can read about it all in following documentation.

https://docs.python.org/library/stdtypes.html#string-methods.

Looping through Strings

A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. One way to write a traversal is with a while loop:

fruit = "aaple"
index = 0
while index < len(fruit):
    letter = fruit[index]
    print(letter)
    index = index + 1

The above program will return:

a
a
p
l
e

This loop traverses the string and displays each letter on a line by itself. The loop condition is index < len(fruit), so when index is equal to the length of the string, the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index len(fruit)-1, which is the last character in the string.

Don't have knowledge about loops, don't worry we will learn about loops in next blog😄. Liked this article. Follow me on twitter 🐦

References

(Dr. Charles R. Severance, Python for Everybody : Exploring Data Using Python 3 , Chapter 6: Strings)