+ 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.
24 ответов
+ 8
What programming language will you use?
What you are asking for is a so called RLE (Run Length Encoded) conversion.
+ 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.
+ 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?
+ 2
Dmitry Bezruchko Show Ur attempt
And our community try to fix it
+ 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='')
+ 1
Louis Thank you very much! My head already hurts from this task! I ran into a wall and cannot move on!
0
I am sorry! Python 3!
0
But the fact is that it is in Python! The point is how to do this with loops?
0
And how do you insert a link to the one you want to answer?
0
Abhay Lothar Jan Markus Hooray! I understand how to give a link! Thank you all very much for participating!
0
Jan Markus Heck! It is so simple! Thank you so much! I already broke my head!
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?
0
Lothar Like in # 2
0
If like # 1, then adding to the dictionary works well. So suggested Jan Markus
0
Lothar So I asked how to do this using loops. But how? I do not understand!
0
By simply use len() function
0
As you mentioned Python,you can use len()
Eg.len(' abcd') will give 5 characters .You can definitely give it a try.
0
The function len("xyz......") can do that for you
0
in c language
you can use "strlen(variable name)" function
https://youtu.be/VMNdaevctMU
thanku