How to Print String and Variable on Same Line in R
Last Updated :
23 Jul, 2025
Printing a string and a variable on the same line is useful for improving readability, concatenating dynamic output, aiding in debugging by displaying variable values, and formatting output for reports or user display.
Below are different approaches to printing String and Variable on the Same Line using R Programming Language.
Table of Content
Printing a string and a variable on the same line Using paste() function
paste() is a function in R that is used for concatenating strings. paste() function can concatenate multiple arguments into a single character vector.
Syntax:
paste(..., sep = " ")
sep : A character string to separate the concatenated elements.
This example prints the string and variable on the same line using paste() funtion.
R
# Define variables
est <- 2008
founder <- "Sandeep Jain"
# Concatenate strings and variables using paste()
paste("GeeksforGeeks was founded in", est, "by", founder)
Output:
[1] "GeeksforGeeks was founded in 2008 by Sandeep Jain"
Printing a string and a variable on the same line Using sprintf() function
sprintf() in R is a function used for formatting strings. sprintf() allows us to create strings with placeholders that can be replaced by specified values. This function is similar to printf() in C programming.
Syntax:
sprintf(fmt)
fmt: A character string that specifies the format.
Below example shows how we can uses sprintf() function to print String and Variable on Same Line.
R
# Assigning values to variables
est <- 2008
founder <- "Sandeep Jain"
# Using sprintf to format a string
sprintf("GeeksforGeeks is a computer science portal founded by %s in %d", founder, est)
Output:
[1] "GeeksforGeeks is a computer science portal founded by Sandeep Jain in 2008"
Printing a string and a variable on the same line Using cat() function
In this approach we will use the cat() function in R which is used to concatenate and print its arguments.
This example uses cat() function to concatenates string and variables.
R
# Define a variable
name <- "Yuvraj"
age <- 22
# Print a string and the variable on the same line using cat
cat("My name is", name, "And i am ", age, "years old")
Output:
My name is Yuvraj And i am 22 years old
Conclusion
We seen several approaches to print a string and a variable on the same line, Each approach has its advantages, such as simplicity, flexibility, or formatting options. Choose the one that best suits for printing strings and variables together in R.
Similar Reads
How to Create, Rename, Recode and Merge Variables in R Variable manipulation is a key part of working with data in the R Programming Language. These actions, whether they involve adding new variables, renaming old ones, recoding them, or merging them together, are critical for every data analysis process. In this article, we'll delve into the intricacie
3 min read
How to Insert New Line in R Shiny String Inserting a new line in a string within an R Shiny application is a common task, especially when dealing with text outputs in UI components such as text Output, verbatimTextOutput, or even within HTML tags. Understanding how to insert new lines correctly helps improve the readability and organizatio
4 min read
How to reverse a string in R Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a
4 min read
R Variables - Creating, Naming and Using Variables in R A variable is a memory location reserved for storing data, and the name assigned to it is used to access and manipulate the stored data. The variable name is an identifier for the allocated memory block, which can hold values of various data types during the programâs execution.In R, variables are d
5 min read
How to display only Values in Plot in R ? In this article, we will discuss how to display only Values in Plot in R Programming Language. The two different approaches to display only values in the plot as follows: Displaying only values using text() function in Plot.Displaying only values using geom_text() function from ggplot2 Package in Pl
3 min read
How to Use a Variable to Specify Column Name in ggplot in R When working with ggplot2 in R, you might find yourself in situations where you want to specify column names dynamically, using variables instead of hard-coding them. This can be particularly useful when writing functions or handling data frames where the column names are not known in advance. This
4 min read