Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Python For Og Lecture 10 String Slicing

This document discusses string slicing in Python. String slicing allows printing of multiple characters from a string by specifying a start and stop index. It demonstrates slicing a string to print portions like 'res' and 'reserv' from the string 'reservoir'. It also shows using a step argument to slice and print every other character or reverse a string. Finally, it provides an exercise to input a user's age, reverse it, and print the result.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Python For Og Lecture 10 String Slicing

This document discusses string slicing in Python. String slicing allows printing of multiple characters from a string by specifying a start and stop index. It demonstrates slicing a string to print portions like 'res' and 'reserv' from the string 'reservoir'. It also shows using a step argument to slice and print every other character or reverse a string. Finally, it provides an exercise to input a user's age, reverse it, and print the result.

Uploaded by

LIC Helper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory

Python for Oil and Gas

Website - https://petroleumfromscratchin.wordpress.com/

LinkedIn - https://www.linkedin.com/company/petroleum-from-scratch

YouTube - https://www.youtube.com/channel/UC_lT10npISN5V32HDLAklsw

# We use string indexing when we need to print a single charcater &

# we use string slicing when we need to print multiple characters of a string


/
1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory

var = 'reservoir'

# I want to print 'res'

# var_name or string_name[start argument or start index : stop argument]

var[0:3] # 3rd index will not be included

'res'

var[0:2]

're'

'reservoir'[0:3]

'res'

# print 'reserv'

var[0:6]

'reserv'

# print 'ir'

/
var[7:9]
1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory
var[7:9]

'ir'

# r e s e r v o i r

# 0 1 2 3 4 5 6(-3) 7(-2) 8(-1)

var[-2:9]

'ir'

var[7:]

'ir'

var[-2:]

'ir'

var[:3]

'res'
/
1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory

# var_name[start arg:stop arg:step argument]

Step Argument

# [start argument : stop argument: step argument]

name = 'Divyansh'

# by default step is 1

name[0:4:2]

'Dv'

name[0:7:2]

'Dvas'

name[:7:2] /
1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory

'Dvas'

name[::2]

'Dvas'

name[0:5:-1]

''

Let us reverse my name

name[7::-1] # negative step

'hsnayviD'

name[-1::-1]

'hsnayviD'

name[::-1] # By default.

'hsnayviD'

name[::-2] # let's see what happen now

/
1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory

'hnyi'

Let us do a excercise! - Assignment 5

# Make computer ask your age using input function.


# Revrse your age! and print --> 'Reverse of my age is ___'

/
1/29/2021 Python for O&G Lecture 10 - String Slicing - Colaboratory

You might also like