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 in Python by Rishi
17 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 by Kiran

Related Questions In Python

+2 votes
6 answers

Slicing in Python

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

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

DataFrame slicing in Python

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

answered Jul 15 in Python by Sakra
20 views
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,810 points
157 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
99 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 275 views
+4 votes
6 answers
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,520 points
145 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 in Python by Wajiha
• 1,920 points
23 views