10 Feb 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:
1 2 3 4 5 6 7 8 9 10 |
# Our string surrounded by double quotes name <- "Amit" # Display the name name # Display the class of the data type class(name) |
Output
1 2 3 4 |
[1] "Amit" [1] "character" |
In the next example, we will create a string and enclose it with single quotes:
1 2 3 4 5 6 7 8 9 10 |
# Our string surrounded by single quotes subject <- 'Maths' # Display the subject subject # Display the class of the data type class(subject) |
Output
1 2 3 4 |
[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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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 2 3 4 5 6 7 |
[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:
No Comments