Strings are group of characters, Contains any number of characters, letters, 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 below syntax
$variable=[string in quotes]
Left side of =
contains string variable
Right side contains string of characters or 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
supports variable interpolation and backsplash characters. Variables are interpolated and replaced with its value during execution.
It also contains special characters that has different meaning.
For example, \t
is for tab, \n
for new line.
$variable = "John";
print "Welcome, $variable!\n";
Output:
Welcome, John
back quote string are abc
Perl has inbuilt backsplace 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 an string in perl, Please use either print or say functions in Perl. You can use print function by enclosing a string literal or string variable $variable
What are the 4 basic string operations?
There are basically basic operations on String
- Concatenation
- Substring
- trim
- reverse of a string