﻿

# Learn Python Programming: A Beginners Crash Course on Python Language for Getting Started with Machine Learning, Data Science and Data Analytics (Artificial Intelligence Book 1) by R. Russo Russel

Author:R. Russo, Russel [R. Russo, Russel]
Language: eng
Format: epub
Published: 2019-11-04T16:00:00+00:00

Index #

0

1

2

3

4

5

6

7

8

9

10

11

12

String

P

y

t

h

o

n

S

t

r

i

n

g

Index #

-13

-12

-11

-10

-9

-8

-7

-6

-5

-4

-3

-2

-1

Example #1:

To access the first character on the variable string_var (the first character of Python String is “P”), enter the variable name “string_var” and enclose the integer zero (0) inside the index operator or square brackets [].

>>> string_var[0]

'P'

>>>

In this example, the first character of the string “Python String” is “P”. Since the first character takes zero as it’s index number, Python gives you the letter “P” as an answer.

Example #2:

To access the character on index 8, simply enclose 8 inside the square brackets:

>>> string_var[8]

't'

>>>

Since “t” takes 8 as it’s index number, Python gives you the letter “t” as an answer.

Example #3:

To access the character on index 6, an empty space:

>>> string_var[6]

' '

>>>

Since an empty space takes 6 as it’s index number, Python gives you ' ' (a space) as an answer.

Example # 4:

To access the last character of the string, you can use negative indexing in which the last character takes the -1 index.

>>> string_var[-1]

'g'

>>>

A string is an ordered list, so you can expect that the penultimate letter takes the -2 index and so on.

Hence, -5 index is:

>>> string_var[-5]

't'

>>>

The Len() Function

There is a more sophisticated way of accessing the last character and it will prove more useful when you’re writing more complicated programs: the len() function.

The len() function is used to determine the size of a string, that is, the number of characters in a string.

For example, to get the size of the variable ‘string_var’, you’ll use the syntax:

>>>len(string_var)

13

>>>

By using the len() function, Python is able to calculate the number of characters in your string “Python String”. Do not forget that Python calculates a space as a character. Thus you get 13 characters.

Since the last character in the string takes an index which is one less than the size of the string, you can access the last character by subtracting 1 from the output of the len() function.

To illustrate, type the following on the command prompt:

>>> string_var[len(string_var)-1]

'g'

>>>

Some important notes about accessing strings through indexing:

● Always use an integer to access a character to avoid getting TypeError.

● Attempting to access a character which is out of index range will result to

an IndexError.

Slicing Strings

You can access a range of characters in a string or create substrings using the range slice [:] operator. To do this interactively on a random string, simply type the string within single or double quotes and indicate two indices within square brackets. A colon is used to separate the two indices. The slice operator will give you a string starting with S[A] and ending with S[B-1].

The syntax is: S[A:B-1]

S: The string you wish to use

A: The starting character of the substring you want to create

B: The ending character of the substring you want to create

Examples #1:

>>>"String Slicer "[2:12]

'ring Slice'

>>>