![]() |
VOOZH | about |
In R, printing a new line within a string is a common task, especially when formatting output for better readability or organizing information, we will discuss how to print a new line in a string using R Programming Language.
In this method, the need to call the cat() function which is here responsible to print text and text.
Syntax:
cat(text/file = ββ, sep = β β, fill = FALSE, labels = NULL, append = FALSE)
Parameters:
Output:
GEEKS FOR GEEKS
GEEKS
FOR
GEEKS
GEEKS
FOR
GEEKSIn this example, the \n character is used to break the string into multiple lines. The output shows the strings printed with line breaks where specified.
The writeLines() function is another useful function in R to write text line by line. Itβs especially useful for outputting strings with newline characters.
Syntax:
writeLines(text, con = stdout(), sep = "\n", useBytes = FALSE)
Parameters:
GEEKS FOR GEEKS GEEKS FOR GEEKS
If you want more precision on how the new lines are inserted, you can define a custom function that splits the string and prints every piece on a separate line. The following example uses the strsplit() function to split the string into characters and print them line by line whenever it hits spaces.
GEEKS FOR GEEKS