Python Slicing operation

0 votes

Hi  Team ,

a="$2.39"

print(a[1:-1])

Output - 2.3

Now 

import pandas as pd

rest_data = pd.read_table("rest_data.tsv.txt", sep = '\t')

for i in rest_data["item_price"]:

    print(i[1:-1])

Output- 2.39

Question is in slicing operation only.  I have applied the same slicing in both the case but the result varies. Can you explain why?

Jul 16, 2019 in Python by Rishi
747 views

1 answer to this question.

0 votes

The difference in the output which you are seeing is because in your data variable "item_price" is having a space character after each price. So the slicing removes only space from the end of the price but in your first example "a" is not having space at the end of it and that's why the difference in the output

answered Jul 16, 2019 by Kiran

Related Questions In Python

+2 votes
6 answers

Slicing in Python

Index: ------------> ...READ MORE

answered Oct 18, 2018 in Python by abc
1,724 views
0 votes
1 answer

DataFrame slicing in Python

You can use criteria based subset for ...READ MORE

answered Jul 15, 2019 in Python by Sakra
783 views
0 votes
1 answer

How to reverse a string in Python using slicing operator?

Hi@akhtar, Strings can be reversed using slicing. To ...READ MORE

answered Aug 17, 2020 in Python by MD
• 95,460 points
3,796 views
0 votes
2 answers
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,466 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,126 views
0 votes
1 answer

Question on PyQt: How to connect a signal to a slot to start a background operation in Python

It shouldn't matter whether the connection is ...READ MORE

answered Nov 27, 2018 in Python by Nymeria
• 3,560 points
1,744 views
0 votes
1 answer

Slicing a list using a variable, in Python

Yes its possible. Use the following piece ...READ MORE

answered Jul 2, 2019 in Python by Wajiha
• 1,960 points
2,080 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP