In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to achieve two string multiplication in python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve two string multiplication in python" can help you solve the problem.
Multiply two strings, the basic idea is to multiply num1 by the digits of num2 in turn, and then add them, as shown in the following figure:
It should be noted that, for the product of high positions, the number of num2 0 after the complement 0 is related to the number of zeros in ten digits, and 2 zeros in 100 digits, assuming that the length of num2 is n, and its digital numbers from left to right are 0, 1, 2. I, the summary rule is: the number of complement 0 = nMui 1 Mui.
The following is the specific code:
# two strings multiply # the basic idea is that num1 multiplies the numbers on each digit of num2 in turn, and then accumulates from add_strings import add_strings1 # add_strings1 to make the two strings add, you can refer to the previous article def mutiply_strings (num1,num2): res ='# the final result len_num1 = len (num1) len_num2 = len (num2) # makes num1 from left to right (direction doesn't matter As long as you define the weight of each digit, multiply it by num2 digits, and finally add for i in range (len_num2): carry = 0 # carry w = len_num2-1 Murray I # weight There are several zeros that need to be added after the result of the calculation curRes = for j in range (len_num1) # the result of this operation: X = num1 [len _ num1-1murj] # reverse order Because num1 has to multiply from right to left product = (ord (x)-ord ('0')) * (ord (num2 [I])-ord ('0')) tmp = str ((product+carry)) carry = int ((product+carry) / 10) curRes = tmp + curRes if carry: # add curRes = str ( Carry) + curRes # print (curRes) res = add_strings1 (res) CurRes) # accumulate the result of each layer return res if _ _ name__ ='_ _ main__': print (mutiply_strings ('234)) print (mutiply_strings (' 123456789)) print (123456789) 987654321)
This is the end of the introduction to "how to multiply two strings in python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.