String: Difference between revisions

From Computer Science Wiki
m (added reference for python)
 
(6 intermediate revisions by one other user not shown)
Line 3: Line 3:
In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.<ref>https://en.wikipedia.org/wiki/String_(computer_science)</ref>.
In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.<ref>https://en.wikipedia.org/wiki/String_(computer_science)</ref>.


Because strings are a sequence of characters, we can usually select them in the same way we can select elements from an array. For example:
Because strings are a sequence of characters, we can select them in the same way we can select elements from an array. For example:


<syntaxhighlight lang="python3">
<syntaxhighlight lang="python3">
Line 21: Line 21:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
<?php
<?php
$a = "Hello World";
$myString = "Hello World";
?>
?>
</syntaxhighlight>
</syntaxhighlight>
Line 27: Line 27:
=== Python ===
=== Python ===


* Single quotes: 'allows embedded "double" quotes'
* Single quotes: 'allows embedded "double" quotes'<ref>https://www.scaler.com/topics/python/strings-in-python/</ref>
* Double quotes: "allows embedded 'single' quotes".
* Double quotes: "allows embedded 'single' quotes".
* Triple quoted: '''Three single quotes''', """Three double quotes"""
* Triple quotes: Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.<ref>https://docs.python.org/3/library/stdtypes.html</ref>
Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.<ref>https://docs.python.org/3/library/stdtypes.html</ref>


<syntaxhighlight lang="python3">
<syntaxhighlight lang="python3">
Line 37: Line 36:


=== Javascript ===
=== Javascript ===
* The use of double quotation marks allows you to use single quotation marks without having to escape them.
* The use of single quotation marks allows you to use double quotation marks without having to escape them.
Please see the example below <ref>https://stackoverflow.com/a/242833</ref>:
alert('Say "Hello"');
alert("Say 'Hello'");
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
// decimal number:
var myString = "Hello World";  
var a = 1234.65;
// a negative number:
var a = -1234.65;  
</syntaxhighlight>
</syntaxhighlight>
== Do you understand this? ==


== Standards ==  
== Standards ==  

Latest revision as of 16:35, 22 March 2022

Programming basics[1]

In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.[2].

Because strings are a sequence of characters, we can select them in the same way we can select elements from an array. For example:

myString = 'Hello World'
print(mystring[2])

# this program would output the letter l (L) .

Example[edit]

Here we encounter some different syntax rules. Programming languages define strings differently. Please do pay attention to the quotation marks, double quotation marks, and triple quotation marks.

PHP[edit]

Please click the following link to read the official PHP page regarding single quotes, double quotes, heredoc sytnax and nowdoc syntax

<?php
$myString = "Hello World";
?>

Python[edit]

  • Single quotes: 'allows embedded "double" quotes'[3]
  • Double quotes: "allows embedded 'single' quotes".
  • Triple quotes: Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.[4]
myString = 'Hello World'

Javascript[edit]

  • The use of double quotation marks allows you to use single quotation marks without having to escape them.
  • The use of single quotation marks allows you to use double quotation marks without having to escape them.

Please see the example below [5]:

alert('Say "Hello"');
alert("Say 'Hello'");
var myString = "Hello World";

Standards[edit]

  • Define the terms: variable, constant, operator, object.

References[edit]