Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet34/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   30   31   32   33   34   35   36   37   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

String Slicing


I want to just quickly introduce the topic of slicing. Slicing is used mostly with Python lists; however, you can use it on strings as well. Slicing is essentially when you only want a piece of the variable, such that if I only wanted “He” from the word “Hello”, we would write the following:

print( word[ 0 : 2 ] ) # will output 'He'

The first number in the bracket is the starting index; the second is the stopping index. We will touch on this concept in a later week; however, feel free to mess around with slicing. Before the day ends though, I’d like to quickly cover the start, stop, and step arguments when slicing. The syntax for slicing is always
>>> variable_name[ start : stop : step ]

In the previous cell, we only included the start and stop because the step is optional and defaults to incrementing by one each time. However, what if we wanted to print every other letter:

print( word[ 0 : 5 : 2 ] ) # will output 'Hlo'

Go ahead and run the cell. By passing the step as the number two, it increments the index by two each time instead of one. We will cover this more in depth in a later chapter; for now let this be an introduction into slicing with all three arguments.

WEDNESDAY EXERCISES


1 . Variable Injection: Create a print statement that injects an integer, float, boolean, and string all into one line. The output should look like “23 4.5 False John”.
2. Fill in the Blanks: Using the format method, fill in the following blanks by assigning your name and favorite activities into variables:
"{ }'s favorite sports is { }."
"{ } is working on { } programming!"
We covered some key concepts when working with strings today, formatting and indexing. Tomorrow we’ll use other methods that will help us manipulate strings.
Thursday: String Manipulation
In many programs that you’ll build, you’re going to want to alter strings in one way or another. String manipulation just means that we want to alter what the current string is.
Luckily, Python has plenty of methods that we can use to alter string data types.
To follow along, let’s continue from our previous notebook file “Week_02” and simply add a markdown cell at the bottom that says, “Manipulating Strings.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   30   31   32   33   34   35   36   37   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish