Strings
How to store words or text
Strings are data types used to store words or text. They are easily spotted in code because string values normally have quotation marks around them. One of the first lines of code one may write when learning to code is printing the famous "Hello World!"
Code:
print("Hello World!")
Output:
> Hello World!
Code:
System.out.println("Hello World!");
Output:
> Hello World!
Code:
cout << "Hello World!" << endl;
Output:
> Hello World!
Notice how in each print statement the string that was printed, "Hello World!" needed to be surrounded by quotation marks. The quotation marks are how the computer differentiates a string from other lines of code, such as a variable name.
Code:
x = 5
print("This is the letter: " + "x")
print("This is the variable: " + str(x))
Output:
> This is the letter: x
> This is the variable: 5
Code:
int x = 5;
System.out.println("This is the letter: " + "x");
System.out.println("This is the variable: " + x);
Output:
> This is the letter: x
> This is the variable: 5
Code:
int x = 5;
cout << "This is the letter: " << "x" << endl;
cout << "This is the variable: " << x << endl;
Output:
> This is the letter: x
> This is the variable: 5
Warning: In the case that there are no quotation marks around what is supposed to be a String data type, the computer will read that phrase as a normal code, which can result in compilation or logic errors. Make sure to use quotation marks to indicate that a piece of code is meant to be a String!
Strings are stored differently in different languages. For example, strings in Java are stored as their own objects of the Java Class, while strings in C/C++ are stored as arrays(lists) of characters. This leads to differences in how Strings can be accessed and manipulated. If we wanted to print the first letter of a string, we would have to do it differently in each language.
Code:
ourString = "hi"
print(ourString[0:1])
Output:
> h
Code:
String ourString = "hi";
System.out.println(ourString.substring(0, 1));
Output:
> h
Code:
string ourString = "hi";
cout << ourString.substr(0, 1) << endl;
Output:
> h
Warning: in Java, strings are not primitive objects, so you will have to capitalize the string type when you declare a variable.
The letter 'h' of "hi" is called a substring of the entire string, and as its name suggests, it is part of the string. Notice how you have to call a method substring() on the String type variable in Java to access it because it is an object, while in Python and C++ you directly access the array of characters that stores the string. You can use this to access parts of a string, getting substrings of different lengths. If we wanted to gather the substring "you d" from the string "How are you doing", we would code
Code:
question = "How are you doing"
print(question[8:13]) # first parameter is start position (inclusive), second parameter is end position (exclusive)
Output:
> you d
Code:
String question="How are you doing";
System.out.println(question.substring(8,  13));  // first parameter is start position (inclusive), second parameter is end position (exclusive)
Output:
> you d
Code:
string question="How are you doing";
cout<<question.substr(8,5)<<endl; // first parameter is start position (inclusive), second parameter is substring length
Output:
> you d
Note: In Python and Java, you would use the positions of the first character you want to include and the one index more than the last character you want to include. However, in C++, the second parameter is instead the length of the substring you are creating.
Keep in mind that despite their differences, strings are just arrays of characters, so remember that the first letter of a string is stored at an index of 0 like an array in most languages.
Strings are also immutable in many languages, which means that once you create a data of the type string, you cannot change it. Thus, if you want to manipulate strings, you may need to access the original string while using other strings and methods. You can perform many more functions on strings, due to them having many attributes, and a common field used when working with strings is their length.
Code:
x = "1234567"
xLength = len(x)
print(xLength)
Output:
> 7
Code:
String x = "1234567";
int xLength = x.length();
System.out.println(xLength);
                                
Output:
> 7
Code:
string x = "1234567";
int xLength = x.length();
cout << xLength << endl;
Output:
> 7
You can play around with all the code we've used in this article on Replit:
Copyright ©2023 Howard County Hour of Code