Ruby Academy · Lesson

String Basics and Methods

Common string operations.

Lesson 1 of 413 steps

String Basics and Methods is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

What Is a String?

A String is a sequence of characters. In Ruby you create one with single or double quotes.

  • 'hello' is a single-quoted string
  • "hello" is a double-quoted string

Strings are everywhere: names, messages, file contents and more.

greeting = "Hello, Ruby!"
puts greeting

String Length

Use length (or its alias size) to count the characters in a string.

word = "banana"
puts word.length
puts word.size

Changing Case

Ruby gives you handy methods to change letter case:

  • upcase makes everything uppercase
  • downcase makes everything lowercase
  • capitalize uppercases only the first letter
name = "ruby LANG"
puts name.upcase
puts name.downcase
puts name.capitalize

Trimming Whitespace

User input often has extra spaces. Use strip to remove leading and trailing whitespace. lstrip and rstrip trim only one side.

messy = "   hello   "
puts messy.strip
puts "[" + messy.strip + "]"

Reversing a String

The reverse method returns the characters in reverse order. It is great for palindrome checks.

word = "level"
puts word.reverse
puts word == word.reverse

Accessing Characters

You can read a single character or a range using index brackets. Indexes start at 0. Negative indexes count from the end.

text = "Ruby"
puts text[0]
puts text[-1]
puts text[0, 2]

Concatenation

Join strings together with + or append in place with << (the shovel operator).

first = "Ruby"
last = "Lang"
puts first + " " + last

buffer = "Hello"
buffer << ", world"
puts buffer

Checking Content

Ask questions about a string:

  • include? checks for a substring
  • start_with? and end_with? check edges
  • empty? checks for an empty string
phrase = "learning ruby"
puts phrase.include?("ruby")
puts phrase.start_with?("learn")
puts "".empty?

Repeating Strings

Multiply a string with * to repeat it. This is handy for separators or simple drawings.

puts "=" * 10
puts "ab" * 3

Replacing Characters

The tr method translates characters one-to-one. To swap whole substrings you use gsub, covered in a later lesson.

code = "hello"
puts code.tr("l", "L")
puts code.tr("el", "ip")

Converting to Numbers

Strings that hold digits can become numbers with to_i (integer) or to_f (float). Non-numeric text becomes 0.

puts "42".to_i + 8
puts "3.14".to_f * 2
puts "abc".to_i

Quick Check

Test your understanding of basic string methods.

Recap

You learned core String tools:

  • length/size for counting
  • upcase, downcase, capitalize for case
  • strip for whitespace, reverse to flip
  • indexing, +, <<, include?, to_i/to_f

Next you will build strings from values using interpolation.

summary = "strings are fun"
puts summary.capitalize.length
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “String Basics and Methods” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “String Basics and Methods”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “String Basics and Methods”?

Common string operations. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “String Basics and Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics and Methods
  2. Interpolation and Formatting
  3. Searching and Replacing
  4. Splitting and Joining
← Back to Ruby Academy