Categories

Friday, March 29, 2024
#919814419350 therichposts@gmail.com
Python

How to convert a string to an integer in Python?

How to convert a string to an integer in Python?

Hello friends, welcome back to my blog. Today this blog post I will tell you, How to convert a string to an integer in Python?

Guy’s if you are new in Python then please check below link for more Python tutorials:

  1. Python Tutorials

Guy’s here is the working code snippet and please use it carefully:

1. Guys the most efficient way of doing this with loop over the string and then use sum function to add them all up:

string = "Rob"
ord_sum = sum(ord(c) for c in string)
print(ord_sum)

2. Guys here is second way with custom encoder decoder function made:

def Encode(input):
chars = ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z", " "]
Index = 0
Result = ""
for char in input:
    Index = chars.index(char) + 10
    Result = Result + str(Index)
return Result

def Decode(input):
chars = ["A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z", " "]
Index = 0
Index2 = "0"
Check1 = 0
Check2 = 1
Result = ""
for char in input:
    try:
        Index2 = str(input[Check1]) + str(input[Check2])
        Index = int(Index2)
        Result = Result + chars[Index - 10]
        Check1 = Check1 + 2
        Check2 = Check2 + 2
    except:
        break
return Result

print(Encode("test"))
print(Decode("49194749"))

This is it guy’s. Guy’s if you have any kind of query then please comment below. Guy’s I will come with more demos soon.

I will appreciate that if you will tell your views for this post. Nothing matters if your views will be good or bad because with your views, I will make my next posts more good and helpful.

Jassa

Thanks

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.