Python 3 How to count the number of characters in a string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python 3 How to count the number of characters in a string?

How to count the number of characters in a string? I need to get this "aabbcc from this "a2b2c2". I roughly understand that this should be a loop inside a loop.

29th May 2020, 2:48 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
24 Answers
+ 8
What programming language will you use? What you are asking for is a so called RLE (Run Length Encoded) conversion.
29th May 2020, 2:57 PM
Lothar
Lothar - avatar
+ 8
Dmitry Bezruchko, Thanks. It can be done by using loops with a counter, regex is also possible. A solution is also to use itertools groupby.
29th May 2020, 5:57 PM
Lothar
Lothar - avatar
+ 7
Dmitry Bezruchko, it still not total clear how the characters should be counted. Input: "aabxcayyyfa" ( spaces are inserted for better readability) (1) a4 b1 x1 c1 y3 f1 # this counted in the complete string (2) a2 b1 x1 c1 a1 y3 f1 a1 # this is counted as consecutive characters Is it like sample 1 or 2 or something else?
29th May 2020, 5:15 PM
Lothar
Lothar - avatar
+ 2
Dmitry Bezruchko Show Ur attempt And our community try to fix it
29th May 2020, 2:52 PM
Abhay
Abhay - avatar
+ 2
Here are some examples of Run Length Encoding. some also show reverse. https://www.sololearn.com/Discuss/1017933/?ref=app #To get this "aabbcc from this "a2b2c2". inp = 'a2b2c2d11e22' print(inp) alph = [k for k in inp if k.isalpha()] num = [k for k in (''.join((m,' ')[m.isalpha()] for m in inp)).strip().split(' ')] print(*[i*int(j) for i,j in zip(alph,num)],sep='')
30th May 2020, 8:37 PM
Louis
Louis - avatar
+ 1
Louis Thank you very much! My head already hurts from this task! I ran into a wall and cannot move on!
1st Jun 2020, 11:32 AM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
I am sorry! Python 3!
29th May 2020, 3:49 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
But the fact is that it is in Python! The point is how to do this with loops?
29th May 2020, 3:53 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
And how do you insert a link to the one you want to answer?
29th May 2020, 3:58 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
Abhay Lothar Jan Markus Hooray! I understand how to give a link! Thank you all very much for participating!
29th May 2020, 4:09 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
Jan Markus Heck! It is so simple! Thank you so much! I already broke my head!
29th May 2020, 4:36 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
Jan Markus But no! At first I was delighted, but then it turned out that if the characters are repeated then the code does not work. And they are repeated. How then to be?
29th May 2020, 4:52 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
Lothar Like in # 2
29th May 2020, 5:19 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
If like # 1, then adding to the dictionary works well. So suggested Jan Markus
29th May 2020, 5:26 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
Lothar So I asked how to do this using loops. But how? I do not understand!
29th May 2020, 6:04 PM
Dmitry Bezruchko
Dmitry Bezruchko - avatar
0
By simply use len() function
30th May 2020, 12:12 PM
Aman Yadav
Aman Yadav - avatar
0
As you mentioned Python,you can use len() Eg.len(' abcd') will give 5 characters .You can definitely give it a try.
30th May 2020, 7:58 PM
swaggy tarret
0
The function len("xyz......") can do that for you
30th May 2020, 10:29 PM
Oni Oluwakayode Michael
Oni Oluwakayode Michael - avatar
0
in c language you can use "strlen(variable name)" function https://youtu.be/VMNdaevctMU thanku
31st May 2020, 2:11 AM
roshan shukla