Strings are a group of characters, that Contains any number of characters, letters, or special characters. It is a scalar type in Perl.

Strings can be declared with $variable. It stores, alphabets, characters, and numbers.

How to define string literals in Perl

String literals are declared using the below syntax.

$variable=[string in quotes]

Left side of = contains string variable The right side contains a string of characters words or sentences enclosed in double quotes.

  • Single quote strings are ‘abc’ It does not support variable interpolation and backsplash characters.

$variable = "John";
print 'Welcome, $variable!\n';

Output:


Welcome, $variable!

  • Double quote strings are "abc": Double quote strings support variable interpolation and backsplash characters. Variables are interpolated and replaced with their value during execution.

It also contains special characters that have a different meaning. For example, \t is for the tab, \n is for the new line.

$variable = "John";
print "Welcome, $variable!\n";

Output:

Welcome, John

The back quote string is abc

Perl has inbuilt backspace characters

  • \n- line break
  • \r - carriege return
  • \t -tab
  • \f - formfeed
  • \b - backspace
  • \\ - backsplash
  • \" - Escape double quote

How do I print a string in Perl?

To print a string in Perl, Please use either print or say functions in Perl. You can use the print function by enclosing a string literal or string variable $variable

What are the 4 basic string operations?

There are basic operations on String.

  • Concatenation
  • Substring
  • trim
  • the reverse of a string