1.4: String Basics
- Page ID
- 117516
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)By the end of this section you should be able to
- Use the built-in
len()function to get a string's length. - Concatenate string literals and variables using the + operator.
Quote marks
A string is a sequence of characters enclosed by matching single (') or double (") quotes. Ex: "Happy birthday!" and '21' are both strings.
To include a single quote (') in a string, enclose the string with matching double quotes ("). Ex: "Won't this work?" To include a double quote ("), enclose the string with matching single quotes ('). Ex: 'They said "Try it!", so I did'.
| Valid string | Invalid string |
|---|---|
"17" or '17'
|
17
|
"seventeen" or 'seventeen'
|
seventeen |
"Where?" or 'Where?'
|
"Where?'
|
"I hope you aren't sad."
|
'I hope you aren't sad.'
|
'The teacher said "Correct!" '
|
"The teacher said "Correct!" "
|
Which of the following is a string?
Hello!29"7 days"
- Answer
-
c. A string that is enclosed by matching double quotes is a valid string. Note that
'7 days'is also a string.
Which line of code assigns a string to the variable email?
"fred78@gmail.com""email = fred78@gmail.com"email = "fred78@gmail.com"
- Answer
-
c. The string,
fred78@gmail.com, is enclosed in matching double quotes, and is assigned to the variableemail. A string can contain characters such as "@" or "." in addition to letters and numbers.
Which is a valid string?
I know you'll answer correctly!'I know you'll answer correctly!'"I know you'll answer correctly!"
- Answer
-
c. The string has matching double quotes. Since a single quote is a part of this string, double quotes must be used.
Which is a valid string?
You say "Please" to be polite"You say "Please" to be polite"'You say "Please" to be polite'
- Answer
-
c. Note that the enclosing single quotes (') are not part of the string but the double quotes (") are.
len() function
A common operation on a string object is to get the string length, or the number of characters in the string. The len() function, when called on a string value, returns the string length.
What is the return value for len("Hi Ali")?
256
- Answer
-
c. The
len()function returns the number of characters in the string. 6 characters exist in"Hi Ali"which are "H", "i", " ", "A", "l", and "i".
What is the length of an empty string variable ("")?
- undefined
- 0
- 2
- Answer
-
b. The length of an empty string equals 0.
What is the output of the following code?
number = "12"
number_of_digits = len(number)
print("Number", number, "has", number_of_digits, "digits.")
Number 12 has 12 digits.Number 12 has 2 digits.Number 12 has number_of_digits digits.
- Answer
-
b. The
number_of_digitsvariable's value is2, and as such, the output is"Number 12 has 2 digits.".
Concatenation
Concatenation is an operation that combines two or more strings sequentially with the concatenation operator (+). Ex: "A" + "part" produces the string "Apart".
Which produces the string "10"?
1 + 0"1 + 0""1" + "0"
- Answer
-
c. The
1and0are enclosed in quotes indicating valid strings, and the + is not, indicating a concatenation operation. Thus, the string"10"is produced. Note that concatenation operator does not include any default separator characters like the space character.
Which produces the string "Awake"?
"wake" + "A""A + wake""A" + "wake"
- Answer
-
c.
"A"and"wake"are valid strings, and the + is not enclosed in quotes, indicating a concatenation operation. Thus the string"Awake"is produced. Note that the concatenation operator does not include any default separator characters like the space character.
A user enters "red" after the following line of code.
color = input("What is your favorite color?")
Which produces the output "Your favorite color is red!"?
print("Your favorite color is " + color + !)print("Your favorite color is " + "color" + "!")print("Your favorite color is " + color + "!")
- Answer
-
c. Note that space characters must be explicitly included when using the concatenation operator.
Which of the following assigns "one-sided" to the variable holiday?
holiday = "one" + "sided"holiday = one-sidedholiday = "one-" + "sided"
- Answer
-
c. A concatenation of the two strings is assigned to the variable
holiday. Note that"one-sided"would assign the same string toholiday.
Write a program that asks the user to input their first and last name separately. Use the following prompts (example input in bold):
What is your first name? Alan What is your last name? Turing
The program should then output the length of each name. Based on the example input above, the output would be:
Your first name is 4 letters long Your last name is 6 letters long
Interactive Code
- Answer
-
first = input("What is your first name? ")
last = input("What is your last name? ")
print("Your first name is", len(first), "letters long")
print("Your last name is", len(last), "letters long")
Write a Python computer program that:
- Assigns the string
"Freda"to a variable,name. - Assigns the string
"happy"to a variable,feel. - Prints the string
"Hi Freda!"with a singleprint()function using the variablename. - Prints the string
"I'm glad you feel happy."with a singleprint()function using the variablefeel.
Interactive Code
- Answer
-
name = "Freda"
feel = "happy"
print("Hi,", name + "!")
print("I'm glad you feel", feel + ".")


