Learn Python Programming - 11 - String Slicing NOT Cake Slicing

preview_player
Показать описание
...
★☆★ FREE Lesson 1: The Most Important Thing For a Successful Programmer★☆★

Enroll for coding exercises, projects, tutorials, and courses...
Clever Programmer
Snapchat ► Rafeh1
Рекомендации по теме
Комментарии
Автор

data = "XBOX 360 | 150 | New"

data_list = data.split(" | ")

# String is converted to list, (" | ") is removed -- ["XBOX 360", "150", "New"]

print(data_list[0])
print(data_list[1])
print(data_list[2])

gibbo_UTV
Автор

It took me an hour, by coincidence I found rindex and boom (I was trying to do it without counting and it succeeded)
data = "Playstation | 250JD | New"

price=
condition= data[(data.rindex("|")+1):]

It was fun thinking for a solution .
Thank you for your hard work #CleverProgrammer

waleadbadri
Автор

yo Qazi. You are a GREAT teacher! I've tried to pick this stuff up for years and have never made this much progress. I am indebted to you for what you're doing bro. Thank You!!

kenwest
Автор

This man is amazing new favorite YouTube I subscribed and rang the bell

ronitgummadi
Автор

Pretty simple tbh! (This is already posted but I will do it anyway)

1. Make up a variable name which equals 'olleh' or 'hello' | Example: name = 'olleh' |
2. Write the code: name::-1 and you will get Hello if your variable was olleh, the opposite if you had hello!

So what does it mean? Welljust think [START:STOP:STEP]. You start at 0 and you dont want to stop in the middle of the word and sthats why i use " : " in my stop section. and at step I want to reverse the word so I write -1. Sorry for my horrible explanation

liam
Автор

Thanks for reminding me and others (hopefully) that it's not good enough to be merely grounded in theory...we must practice practice, practice.

TechNSayge
Автор

Can't agree more with your advice to really do it as we're watching you. It does seem like everything is easy when watching and listening but brain fart happens when asked to write the code later.

I like how you implement "real world" application of the script you showed in your example. It makes programming more tangible and see the potential of what can be done later on.

Can't wait to see your setup and how to write files.

ambermeows
Автор

'hello'


print('hello'[-1::-1])

joelwalberg
Автор

name = 'hello'
c= -1
rev = " "
for i in range(5):
reverse = name[c]
rev = rev + reverse
c= c-1
print (rev)

rohitjoshi
Автор



data= ' Makeup | 350 | New '
data.index('|')
>>>8

>>> Makeup

data.rindex('|')
>>>14



>>>350



>>> New.



**This is how i got it**

hafsahamza
Автор

#printing different product details


data = " JBL Headset | 20 | New "

print(data[:data.index('|')])

cost = data[(data.index('|')+1):]
print(cost[:cost.index('|')])








#Reversing a string

word = "Hello"

print(word[::-1])

kreed
Автор

For olleh, i needed help so I started looking at the comments. Ended up using
>>> "hello"[::-1]

Even though it should have been the same, the following didn't work for some reason.
>>> "hello"[0:5:-1]

Sorting through the prices were a bit trickier though.
>>> product = data[0:data.index('|')-1]
>>> price =
>>> condition = data[(data.rindex('|')+1):]

chaospatriot
Автор

name='mahsa'
i=-1
for a in range(5):
print(name[i])
i=i-1

mahsakarimi
Автор

You, inadvertently, helped me on a problem I have been stuck on for 3 days. Thank you, here's a like.

mylegguy
Автор

reverse:
greeting[::-1]
start= by defaut 0 if it's blank
end= if it's negative, you're slicing in the reverse
-1 = (-) slice from the ending pos, (1) read every other letter

shubhamkanwal
Автор

I just want to say you are making learning fun. I tried codecademy, but lost interest very quickly. Thanks man!

FeralBoyKnives
Автор

I thought of having more than 2 pipes (which would interfere with the rindex method the comment section uses) so I tried it with 3 pipes and this is my code (I used Slicing techniques and Looping):

data = "XBOX 360 | 150 | New | USA"
for entry in ["product", "price", "condition"]:

entry = data[:data.index('|')]
print (entry)
data = data[data.index('|')+2:]

print (data)

alextegrado
Автор

somebody suggest using rindex to find the second '|'.
what if there are more than two in the data
say xbox|350USD|new|made in China
what is the code for searching for the next '|' in the data?

maggiemak
Автор

data = "3DS | 5999 | Broken"

product = data[:data.index("|") - 1]
price = data[data.index("|") + 2:data[data.index("|") + 3:].index("|") + data.index("|") + 2]
condition = data[data[data.index("|") + 1:].index("|") + data.index("|") + 3:]

print(product)
print(price)
print(condition)


This is my way of doing I'm not sure if I overcomplicated it, but I think I did pretty well. It works with whatever you put into it, and there aren't any spaces anywhere, it just gives the 3 outputs without spaces. I think this is the best one in the comment section.

kirasmith
Автор

def reverse(string):
output = ""
for i in range (len(string)):
extracted_string = string[-1:]
output += extracted_string
string = string[:len(string)-1]
return output

print(reverse('hello'))

neupanesamrat