<<

STRINGS IN PYTHON

Muhammad Khokhar MANGO CLOUD TECHNOLOGIES Email: [email protected]

PYTHON PROGRAMMING CHAPTER 02 - STRINGS

Document Revision: Version 1.0 Date: 26th February 2021 Authors: Muhammad Tayyeb Khokhar Distribution: Mango Cloud Technologies

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

DOCUMENT CONTROL & INFORMATION Document Information

Document Reference Strings in Python

File Name Strings in Python.pdf

Print Date 26/02/2021

Last Modified Date 26/02/2021

Current Document Status Published

Revision History

Version Date Revision Details

1.0 26/02/2021 Final

1.1

1.2

Review / Approval

Date Reviewer

01/03/2021 Mango Cloud Technologies

Issued by

Mango Cloud Technologies

[email protected]

www.mangoctech.com

Tel: +44 (0)1905 954701

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Table of Contents

Python Programming ...... 4 Use of Python ...... 4 Python Coding ...... 4 Strings in Python ...... 4 Writing StringS In Python ...... 4 String Indexing ...... 5 String Output ...... 5 Multiple Statements In String ...... 5 String Slicing ...... 6 Immutable Strings ...... 7 String Concatenation ...... 8 Built-in-Functions ...... 8 String Interpolation ...... 9 Floating Interpolation ...... 10

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

PYTHON PROGRAMMING

Python is a widely used general-purpose, high level . It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code. Python is a general-purpose coding language—which means that, unlike HTML, CSS, and JavaScript, it can be used for other types of programming and software development besides web development. That includes back end development, software development, data science and writing system scripts among other things. USE OF PYTHON

 Web Development.  Game Development.  Machine Learning and Artificial Intelligence.  Data Science and Data Visualization.  Desktop GUI.  Web Scraping Applications.  Business Applications.  CAD Applications.  Embadded Applications. PYTHON CODING

Python is easy to learn. Its syntax is easy and code is very readable. ... Python allows you to write programs in fewer lines of code than most of the programming languages. The popularity of Python is growing rapidly. Dependencies. STRINGS IN PYTHON

A string in Python is a sequence of characters. It is a derived data type. Strings are immutable. This means that once defined, they cannot be changed. Many Python methods, such as replace(), join(), or split() modify strings. However, they do not modify the original string. They create a copy of a string which they modify and return to the caller. WRITING STRINGS IN PYTHON

In Python, we can write a string by using single quotes, double quotes or triple around the text. Text could be the numbers or characters. A string is simply a series of characters. Anything inside quotes is considereda string in Python, and you can use single or double or triple quotes around the statement. If we have multiple lines in a string then we have to use triple quotes.

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Table – 01

Examples of String

Single Qoutes ‘I am a single quote string’

Double Qoutes “I’m a double quote string”

Triple Qoutes “‘I am “triple” quote string”’

STRING INDEXING

Every character in a string have index value string from 0. First character in the string will have index value 0, then 1 and then continue. Table – 02

String = ‘I am string’

String character ‘I a m s t r i n g’

Index Values 0 1 2 3 4 5 6 7 8 9 10

Reverse index 0 &-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

Note space in a string is called white spcaing and it also have an index value.

STRING OUTPUT

Table – 03

Examples of String

Cell 1 my_string = ‘I am a python string’

Cell 2 my_string

Output ‘I am a python string’

Cell 3 print(my_string)

Output I am a python string

MULTIPLE STATEMENTS IN STRING

In python, if we want to show multiple statements in the output. We have to use print. Python will only show the last statement if we will not use print command.

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Table – 04

Other Examples of String

Cell 1 'I am a python string'

'I am better than all other programming languages'

'I am easy to learn'

Output ‘I am easy to learn’

Cell 2 print('I am a python string')

print('I am better than all other programming languages')

print('I am easy to learn')

Output I am a python string

I am better than all other programming languages

I am easy to learn

Use \n for next line, \t for Tab spacing and \\ for slash

Cell 3 print('I am python string.\n I am better than other languages \t easy to learn\\')

Output I am python string.

I am better than other languages easy to learn\

STRING SLICING

Slice allows us to grab a subsection of multiple characters, just like a slice of bread or slices of bread.  The syntax is [start:stop:step]  start is a numerical index for the slice.  stop is the index we will go up to (but not include)  step is the size of the “jump” we make Table – 05 Examples to slice a String

Cell 1 A = ‘Slice a string’

Cell 2 A[0]

Output ‘S’

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Cell 3 A[1]

Output ‘l’

Cell 4 A[5]

Output ‘ ’

Cell 5 A[1:]

Output ‘lice a string’

Cell 6 A[:5]

Output ‘Slice’

Cell 7 A[:-7]

Output ‘Slice a’

Cell 8 A[-7:]

Output ‘ string’

Cell 9 A[2:6]

Output ‘ice’

Cell 10 A[0:15:3]

Output ‘Scatn’

Cell 11 A[::-3]

Output ‘gr el’

IMMUTABLE STRINGS

Strings are immutable, which means we cannot change the strings. If we try to change the string we will get type error message. But we have other ways to make changes in the string. Table – 06 Examples to change the string

Cell 1 new_string1 = 'I am immutable, please don\'t change me'

Cell 2 new_string1[0] = 'U'

Output TypeError: 'str' object does not support item assignment

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Note: Use # in the beginning of code to disable the line

STRING CONCATENATION

In python, we cannot change the string but we can use concatenation method to change the strings. Table – 07

Example of string contatenation

Cell 1 my_name = 'Apple Cloud Technologies'

Cell 2 Correct_name = my_name[5:]

Cell 3 'Mango' + Correct_name

Output ‘Mango Cloud Technologies’

Adding strings

Cell 1 A = 'Mango '

B = 'Cloud '

C = 'Technologies'

Cell 2 D = A + B + C

Output ‘Mango Cloud Technologies’

Multiplying strings

Cell 1 My_string = ‘Python ’

Cell 2 My_string * 5

Output ‘Python Python Python Python Python’

BUILT-IN-FUNCTIONS

There are many built-in functions in python for strings. We can press tab to check what possible functions are available with the string. To execute the built-in-function use () and run Table – 08

Built-in-function .upper will change all the alphabets to capital letters

Cell 1 x = 'I am python'

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Cell 2 x.upper()

Output ‘I AM PYTHON’

Built-in-function .lower will change all the alphabets to lowercase

Cell 1 y = ‘PYTHON IS EASY TO LEARN’

Cell 2 y.lower()

Output ‘python is easy to learn’

Built-in-function .split will split the string and convert into list

Cell 1 x.split()

Output ['I', 'am', 'python']

Cell 2 y.split()

Output ['PYTHON', 'IS', 'EASY', 'TO', 'LEARN']

STRING INTERPOLATION

String interpolation is a process substituting values of variables into placeholders in a string. In simple words, injecting values in string. There are two methods for string interpolation.

 .format()  f-strings Table – 09

Examples of Interpolation

Cell 1 print('We are learning')

Cell 2 a = ‘Python’

Cell 3 print('We are learning {}' .format (a))

Output We are learning Python

Another example

Cell 1 print('we are learning {} {} {}' .format (a, a, a) )

Output we are learning Python Python Python

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Another example

Cell 1 b = ‘Programming’

Cell 2 print('we are learning {} {} {}' .format (a, a, b) )

Output we are learning Python Python Programming

Another example

Cell 1 print('we are learning {2} {1} {0}' .format (a, a, b) )

Output we are learning Programming Python Python

Cell 2 print('we are learning {2} {2} {2}' .format (a, a, b) )

Output we are learning Programming Programming Programming

FLOATING INTERPOLATION

Injecting floats in string. Table – 04

Example

Cell 1 Result = 12/555

Output 0.021621621621621623

Cell 2 print(‘The answer is {}’ .format (Result))

Output The answer is 0.021621621621621623

Another way

Cell 3 print(‘The answer is {x}’ .format (x=Result))

Output The answer is 0.021621621621621623

To round up decimal values

Cell 4 print('The answer is {w:1.4f}' .format (w=Result))

Output The answer is 0.0216

To display with multiple spaces.

Cell 5 print('The answer is {w:10.4f}' .format (w=Result))

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com

Output The answer is 0.0216

Cell 1 Name = ‘Mango Cloud Technologies’

Cell 2 Released = 2020

Cell 3 print(f’{Name} launched in {Released} December.’)

Output Mango Cloud Technologies launched in 2020 December.

Another example

Cell 1 Name = Usman

Age = 20

Cell 2 Print(f‘{Name} is {Age} years old.’)

Output Usman is 20 years old.

*** END ***

MCT | Online Business, WR5 1GP Tel: +44 (0)1905 954701 | Web: www.mangoctech.com