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
87 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
376 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
183 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 16, 2020 in Python by MD
• 95,160 points
873 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,032 views
0 votes
0 answers
+4 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,810 points
1,556 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,520 points
874 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,950 points
384 views