Strings and Console Output Flashcards

1
Q

String

A

A data type; can contain letters, numbers, and symbols.

eg. Name = Ryan (Ryan is the string value)
age = 19
food - cheese

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Escaping Characters

A

Characters that cause trouble in code because Python thinks they’re ending the code (the apostrophe is one) - fix this by putting a backslash before them eg. ‘There's)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Index

A

A number assigned to a character in a string. Always starts counting from “0”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

String methods

A

Let you perform certain tasks for strings

Eg. len(), lower(), upper(), str()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

len()

A

string method that gets the length (number of characters) of a string - the variable goes into the ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

lower()

A

String method that removes capitalization in a string - call like variable.lower()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

upper()

A

String method that adds capitalization in a string - call like variable.upper()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

str()

A

A string method that turns non-strings into strings eg. str(2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dot Notation

A

Eg. variable.upper()

The dot that separates them is dot notation and used for upper() and lower() string methods. Methods using dot notation only work with strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Print

A

Used to print strings and variables to the console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

String Concatenation

A

Combining strings together using arithmetic operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explicit String Conversion

A

Used to combine a string with a non-string - need to convert non-string to a string - uses str()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

String formatting with %

A

”%” operator after a string can combine a string with variables

Replaces %s with the string variable that comes after it

Eg. print “Hello %s” % (name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is happening here?

name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print “Ah, so your name is %s, your quest is %s, “ \
“and your favorite color is %s.” % (name, quest, color)

A

String formatting is being used to insert the variables name, quest, and colour into the print command using the %s and % operators. The program will accept the raw input to each question and put it into the print command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

datetime.now()

A

Retrieves the current date and time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

from datetime import datetime

A

imports the datetime library for use in code

17
Q

What is happening here?

from datetime import datetime
now = datetime.now()

print ‘%s/%s/%s’ % (now.month, now.day, now.year)

A

The date time library is being imported
the variable “now” is being set to daytime.now, and one will print using string substitution to list the date by month/day/year