Comparison: Python and Ruby (strings)

Comparison: Python and Ruby (strings)

A documented Python learning curve

ยท

1 min read

I've been Ruby Developer for about 10 years and it's been fun. However, I wanted to try Python always. Even though I had some encounters with this language sometimes, I never had a ramp-up. And, I thought it would be a good idea to document how I learn Python from a Ruby perspective, at least at the beginning.

This is not intended to discuss which language is better. Each one has its own strengths and weaknesses in different contexts. This is more intended to look at the beauty of both and judge by yourself which one you would use in some specific context.

Here we go. Let's start with string manipulation.

Ruby

puts "Hello world"

# string concantenation
puts "Don't worry," + ' be happy'

# interpolation
first = "Don't worry,"
second = 'be happy'
puts "#{first} #{second}"

# replace
puts 'Hello world!'.gsub('world', 'Hashnode')

Python

print("Hello world")

# string concatenation
print("Don't worry," + ' be happy')

# interpolation
first = "Don't worry,"
second = 'be happy'
print(f'{first} {second}')

# replace
print('Hello world!'.replace('world', 'Hashnode'))

JupyterLab side-by-side

Ruby vs Python

ย