What is the meaning of int a -1 in Python duplicate

0 votes

I don't get this at all. In people's coding, I have observed this. yet unable to determine what it does. Python is the language im using: 

str(int(a[::-1]))
Sep 21, 2022 in Python by Samuel
• 460 points
822 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I'll assume that an is a string. Python's Slice notation has the syntax -

list[<start>:<stop>:<step>]

As a result, when you perform a[::-1], each element is taken from the end to the beginning. So it turns an around. This also applies to lists and tuples.

As an illustration

>>> a = '1234'

>>> a[::-1]

'4321'

The string is returned after you convert it to an int and then back to a string (although I'm not clear why you do that).

answered Sep 26, 2022 by anonymous

edited Mar 5
0 votes

I'm not sure i understand why it's written this way but here's what it does. 

`a[::-1]` returns the list `a` in reverse order (`::` means reverse, `-1` means include the zeroth element)

`int()` casts the argument as an integer

`str()` casts its argument as a string

However, the call `str(int(a[::-1]))` fails, because `int()` expects a string, a bytes-like object or a number as its argument, not a list

answered Oct 4, 2022 by glass.ships

edited Mar 5

Related Questions In Python

0 votes
1 answer

What is the meaning of “int(a[::-1])” in Python?

Assumming a is a string. The Slice ...READ MORE

answered Aug 27, 2018 in Python by Priyaj
• 58,020 points
6,614 views
0 votes
1 answer

What is the Python logic to count the number of capital letters in a file?

Hi, You can use this piece of code, ...READ MORE

answered Jun 26, 2020 in Python by Roshni
• 10,480 points
2,379 views
0 votes
1 answer

.What is the meaning of "while True :" while doing in a loop.

The body of the loop will continue ...READ MORE

answered Oct 7, 2020 in Python by Gitika
• 65,770 points
814 views
0 votes
1 answer

What is the data type of (1) in python?

Typecasting is the process of converting one ...READ MORE

answered Jan 4, 2023 in Python by Elton
• 400 points
814 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
5,258 views
0 votes
1 answer

What is the use of raw_input function in Python?

raw_input fuction is no longer available in ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
1,419 views
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,680 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,473 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