R Strings

Strings in the R programming language are surrounded by single or double quotes. Let us see how to create a string and multiline strings in R with examples.

Create a String

In the below example, we will create a string and enclose it with double quotes:

# Our string surrounded by double quotes
name <- "Amit"

# Display the name
name

# Display the class of the data type
class(name)

Output

[1] "Amit"
[1] "character"

In the next example, we will create a string and enclose it with single quotes:

# Our string surrounded by single quotes
subject <- 'Maths'

# Display the subject
subject

# Display the class of the data type
class(subject)

Output

[1] "Maths"
[1] "character"

Create Multiline Strings

To create a multiline string, you need to only mention the string as we saw above. No need to add any new quote or keyword. Let us see an example:

# Our multiline string1
# What is Python
myStr1 <- "Python is a powerful, interpreted, 
object-oriented programming language. 
It is used in many areas for development and
is considered a perfect language for scripting."

# Display the multiline string1
# A the end of each line break, \n gets added to the output
myStr1

# Our multiline string2
# What is Python
myStr2 <- "Python is a powerful, interpreted, 
object-oriented programming language. 
It is used in many areas for development and
is considered a perfect language for scripting."

# Display the multiline string2
# We have used the cat() method
# The cat() displays the string at the exact position in which it is declared 
cat(myStr2)

Output

[1] "Python is a powerful, interpreted, \nobject-oriented programming language. \nIt is used in many areas for development and\nis considered a perfect language for scripting."
Python is a powerful, interpreted, 
object-oriented programming language. 
It is used in many areas for development and
is considered a perfect language for scripting.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

R - Decision Making Statements
R String Operations
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment