← Back to R Course | Chapter 6: Strings | Lesson 4 of 7

toupper and tolower

toupper turns every letter into a capital and tolower turns them into small letters, like a volume knob for text. It helps when you want words to look the same.

In this page:

  1. toupper and tolower
Syntax
r
toupper(string)
tolower(string)

toupper and tolower

toupper() converts a string to all uppercase, and tolower() converts it to all lowercase. Both work element-wise across a vector of strings. They're commonly used for case-insensitive comparisons by normalizing both sides to the same case first.

Note: For case-insensitive comparisons, normalize both strings with tolower() before comparing them with ==.

Example: toupper and tolower

markup
text <- "Hello World"

cat(toupper(text), "\n")
cat(tolower(text), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting toupper(text) to change text, when it returns a new string unless you assign it.
  2. Comparing strings without matching case, such as "Yes" == "yes", which is FALSE; convert with tolower() first.
  3. Using toupper on a number and getting a string, when the value becomes character.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.