What is string.capitalize() in Ruby?

The string.capitalize() function in Ruby does not modify the original string, but instead returns a copy of the original string, in which the first letter is capitalized and the remaining letters are lower case.

Figure 1 shows the visual representation of the string.capitalize() function.

Figure 1: Visual representation of string.capitalize() function

Syntax

string_name.capitalize()
# string_name is the name of the string to be capitalized

Parameter

This function does not require a parameter.

Return value

This function returns the copy of the original string, in which the first letter is capitalized and the remaining letters are lower case.

Example

The following code shows how to use the string.capitalize() function.

# working example of string.capitalize()
print "('hello').capitalize() : ", ('hello').capitalize(), "\n"
print "('educative').capitalize() : ", ('educative').capitalize(), "\n"
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources