Divides the character string by the specified number of characters. In Ruby and Python.

I was writing Ruby recently, and I knew that it was Ruby.

test.rb


s = '123456789'
v = s.each_char.each_slice(3).map(&:join)
# ['123', '456', '789']

Cool ~

Is it like this in Python?

test.py


s = '123456789'
v = [s[i: i+3] for i in range(0, len(s), 3)]
# ['123', '456', '789']

When asked which one is better, I personally prefer Python, which can be combined with list comprehensions and general-purpose mechanisms, but Ruby is easier to understand. It's amazing that there is a method every time.

Recommended Posts

Divides the character string by the specified number of characters. In Ruby and Python.
Count the number of Thai and Arabic characters well in Python
Divide the string into the specified number of characters
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Shift the alphabet string by N characters in Python
Get the last element of the array by splitting the string in Python and PHP
How to quickly count the frequency of appearance of characters from a character string in Python?
Project Euler # 17 "Number of Characters" in Python
Fill the string with zeros in python and count some characters from the string
[Introduction to Python] Thorough explanation of the character string type used in Python!
Specifying the range of ruby and python arrays
plot the coordinates of the processing (python) list and specify the number of times in draw ()
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
Check if the string is a number in python
How to get the number of digits in Python
Store Japanese (multibyte character string) in sqlite3 of python
Difference between Ruby and Python in terms of variables
Get the size (number of elements) of UnionFind in Python
How to identify the element with the smallest number of characters in a Python list?
Check the processing time and the number of calls for each process in python (cProfile)
Find out the apparent width of a string in python
[Python] Open the csv file in the folder specified by pandas
Remove double-byte spaces before and after the character string (python)
Count the number of characters in the text on the clipboard on mac
Get the number of specific elements in a python list
Python --Find out number of groups in the regex expression
[Homology] Count the number of holes in data with Python
[Tips] Problems and solutions in the development of python + kivy
Maximum number of characters in Python3 shell call (per OS)
The story of Python and the story of NaN
Specify the encoding of the unicode string in the Python 2.x print statement
I compared the speed of Hash with Topaz, Ruby and Python
[Understanding in the figure] Management of Python virtual environment by Pipenv
[Python] Let's reduce the number of elements in the result in set operations
Read the standard output of a subprocess line by line in Python
Get the title and delivery date of Yahoo! News in Python
Get the number of readers of a treatise on Mendeley in Python
Conversion of string <-> date (date, datetime) in Python
Check the behavior of destructor in Python
Differences between Ruby and Python in scope
Graph of the history of the number of layers of deep learning and the change in accuracy
How to write the correct shebang in Perl, Python and Ruby scripts
Comparing the basic grammar of Python and Go in an easy-to-understand manner
python> array> Determine the number and initialize> mylist = [idx for idx in range (10)] / mylist = [0 for idx in range (10)] >> mylist = [0] * 10
Change the saturation and brightness of color specifications like # ff000 in python 2.5
# 5 [python3] Extract characters from a character string
Check the in-memory bytes of a floating point number float in Python
Derivation of multivariate t distribution and implementation of random number generation by python
[Python] Calculate the number of digits required when filling in 0s [Note]
The result of installing python in Anaconda
Test & Debug Tips: Create a file of the specified size in Python
I want to batch convert the result of "string" .split () in Python
Read the file line by line in Python
Read the file line by line in Python
The basics of running NoxPlayer in Python
Basic grammar of Python3 system (character string)
Pandas of the beginner, by the beginner, for the beginner [Python]
I tried to verify and analyze the acceleration of Python by Cython
Open an Excel file in Python and color the map of Japan
Get the number of articles accessed and likes with Qiita API + Python
I checked the number of closed and opened stores nationwide by Corona