Python 3 Strings - Operators & Formatting

Python 3: From Scratch to Intermediate BEGINNER LEVEL: Python 3 - Strings
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€94.09
List Price:  €131.73
You save:  €37.64
£80.33
List Price:  £112.47
You save:  £32.13
CA$137.95
List Price:  CA$193.14
You save:  CA$55.18
A$155.90
List Price:  A$218.27
You save:  A$62.36
S$136.45
List Price:  S$191.03
You save:  S$54.58
HK$783.08
List Price:  HK$1,096.34
You save:  HK$313.26
CHF 91.31
List Price:  CHF 127.84
You save:  CHF 36.52
NOK kr1,099.13
List Price:  NOK kr1,538.83
You save:  NOK kr439.69
DKK kr702.04
List Price:  DKK kr982.89
You save:  DKK kr280.84
NZ$169.94
List Price:  NZ$237.92
You save:  NZ$67.98
د.إ367.20
List Price:  د.إ514.10
You save:  د.إ146.89
৳10,968.60
List Price:  ৳15,356.49
You save:  ৳4,387.88
₹8,361.11
List Price:  ₹11,705.89
You save:  ₹3,344.78
RM479.15
List Price:  RM670.83
You save:  RM191.68
₦120,104.65
List Price:  ₦168,151.32
You save:  ₦48,046.66
₨27,798.84
List Price:  ₨38,919.49
You save:  ₨11,120.65
฿3,667.03
List Price:  ฿5,133.99
You save:  ฿1,466.96
₺3,248.77
List Price:  ₺4,548.41
You save:  ₺1,299.64
B$518.45
List Price:  B$725.86
You save:  B$207.40
R1,905.92
List Price:  R2,668.37
You save:  R762.44
Лв184.07
List Price:  Лв257.70
You save:  Лв73.63
₩139,257.60
List Price:  ₩194,966.22
You save:  ₩55,708.61
₪377.23
List Price:  ₪528.13
You save:  ₪150.90
₱5,695.28
List Price:  ₱7,973.62
You save:  ₱2,278.34
¥15,458.12
List Price:  ¥21,641.98
You save:  ¥6,183.86
MX$1,680.99
List Price:  MX$2,353.45
You save:  MX$672.46
QR365.97
List Price:  QR512.38
You save:  QR146.40
P1,384.25
List Price:  P1,938
You save:  P553.75
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,851.82
List Price:  E£6,792.74
You save:  E£1,940.92
ብር5,709.83
List Price:  ብር7,993.99
You save:  ብር2,284.16
Kz83,396.59
List Price:  Kz116,758.56
You save:  Kz33,361.97
CLP$97,269.27
List Price:  CLP$136,180.87
You save:  CLP$38,911.60
CN¥723.78
List Price:  CN¥1,013.33
You save:  CN¥289.54
RD$5,930.22
List Price:  RD$8,302.55
You save:  RD$2,372.32
DA13,484.73
List Price:  DA18,879.17
You save:  DA5,394.43
FJ$227.82
List Price:  FJ$318.96
You save:  FJ$91.14
Q778.21
List Price:  Q1,089.53
You save:  Q311.31
GY$20,908.09
List Price:  GY$29,272.17
You save:  GY$8,364.07
ISK kr14,179.58
List Price:  ISK kr19,851.98
You save:  ISK kr5,672.40
DH1,015.88
List Price:  DH1,422.27
You save:  DH406.39
L1,779.82
List Price:  L2,491.82
You save:  L712
ден5,791.90
List Price:  ден8,108.89
You save:  ден2,316.99
MOP$805.91
List Price:  MOP$1,128.31
You save:  MOP$322.39
N$1,902.84
List Price:  N$2,664.06
You save:  N$761.21
C$3,694.12
List Price:  C$5,171.92
You save:  C$1,477.79
रु13,341.25
List Price:  रु18,678.28
You save:  रु5,337.03
S/371.93
List Price:  S/520.72
You save:  S/148.78
K379.86
List Price:  K531.82
You save:  K151.96
SAR375.07
List Price:  SAR525.11
You save:  SAR150.04
ZK2,516.51
List Price:  ZK3,523.21
You save:  ZK1,006.70
L468.24
List Price:  L655.55
You save:  L187.31
Kč2,375.47
List Price:  Kč3,325.75
You save:  Kč950.28
Ft37,163.97
List Price:  Ft52,031.05
You save:  Ft14,867.07
SEK kr1,095.61
List Price:  SEK kr1,533.90
You save:  SEK kr438.29
ARS$86,820.40
List Price:  ARS$121,552.04
You save:  ARS$34,731.63
Bs693.54
List Price:  Bs970.99
You save:  Bs277.44
COP$387,000.37
List Price:  COP$541,816
You save:  COP$154,815.63
₡50,265.75
List Price:  ₡70,374.06
You save:  ₡20,108.31
L2,477.69
List Price:  L3,468.86
You save:  L991.17
₲740,341.68
List Price:  ₲1,036,507.98
You save:  ₲296,166.29
$U3,889.78
List Price:  $U5,445.86
You save:  $U1,556.07
zł407.99
List Price:  zł571.20
You save:  zł163.21
Already have an account? Log In

Transcript

What else can we do with strings, for instance, we can concatenate them. concatenation means unifying two or more strings into a single string. You can do this using the plus operator, like you would when adding two numbers. So let's try this. Let's set x with a value of Cisco, and y with a value of space switch. And finally, let's add them together.

So x plus y, Cisco switch and the concatenation has been successfully completed. Another thing we can do is string repetition by using the multiplication operator. So x times three returns Cisco three times which is again correct nice. You can also verify if a character is in a string or not using the in and not in operators. This may prove To be useful when dealing with huge strings. So let's look at x again.

And check if the character O is part of this string. So Oh, in between double quotes in x returns through. Great. Now let's try another one. For example, B in x returns false because there is no letter B in x. But if we use B not in x, this will result in true because we negate the existence of letter B in Cisco, which is indeed true.

Now, what's the deal with string formatting? Let's say we have some kind of string template, and we want to constantly modify only a few words inside the text. But keep the overall template the same. To see what I mean. Let's just assume we have the following string. Doesn't matter what it stands for.

Just focus on the string itself. We Want to keep this string as a template and just change the model name for the Cisco router, the number of slots and the iOS version a couple of times when running our Python program. So this would need to be a dynamic change each time. For this, we should use the percent operator followed by s for string, D four digit, or F for a floating point number. Let's see the syntax for this. Now let's translate this.

The percent s means that this is a placeholder for a string, we will specify in between parentheses at the end of this line. The percent D operator follows the same logic but for a number instead of a string. And finally, the percent f right here refers to a floating point number, a number with a decimal point. Now moving on the fourth value from within the parentheses. This one is going to be associated with the first format operator industry. During the present as the second value from within the parentheses is going to be associated with the second format operator in the string, the percent D, and so on for all the format operators you have in your string.

Also, do not forget to insert the percent sign between the string and the parentheses containing the values. This operator maps the format operators inside the string with the values inside the parentheses. So let's see this in action. Let me hit enter. Now let's change the values a couple of times. To prove that the template remains unchanged, only the values are changing.

Something you might have noticed is the addition of several decimal plates. When dealing with floating point numbers. In order to control this behavior, you can easily choose the number of decimal places that you want to print out. Let's get back to our string and simply insert a dot enter value in between the percent operator And the letter F. If we want just a single digit after the floating point, we should use dot one like this. For two digits, we should use dot two, of course. Finally, if we want no digits at all, and no floating point, we can just enter a dot and we will get the value 15 in return.

So let's try this dot F. Okay, that's awesome, right? However, this is not the only way of dealing with string formatting. Instead of formatting operators, like the ones we've just seen, we can use another notation replacing percent s percent D or percent F, with a pair of curly braces. Also, after the string, the percent operator we used for mapping the values is going to be replaced by a method called format. Let's type this in. So what we did is we replaced the old formatting operators with curly braces and Right after the string, we use a dot, the Word format, which is the name of the method, and then the content inside the parentheses stays the same.

Again, we're mapping each pair of curly braces inside the string template with each value inside the parentheses of the format method. And the result is the same. Is this all there is to it? Well, no, it isn't. What else, we can also use some sort of indexing when dealing with this type of string formatting. Why?

You'll see why in just a few moments. Let's assign a value for each of these pairs of curly braces. Okay, so if we run this line of code, the result is the same. That's because zero right here is mapped to the first value in between parentheses, one is mapped to the second value and so on. Nothing unusual here. So why do we need these values these indexes after all, let's say that maybe you want to switch the order in which the values on the right are mapped.

To the operators inside the string. What will happen if we change our code like this? Let me type this in. Well, we are noticing that we have easily switched the order of values inside our string using this simple trick right here by switching the values the indexing as well. Although the mapping is still the same, meaning zero still corresponds to 2600 Xm, because it is the first element inside these parentheses, its position inside the string has changed according to our needs. Another thing we can do is value repetition inside the string template.

So for instance, if we want 12 dot for being printed instead of 2600 exam, we can just use it twice in our string, once in its initial position and once instead of the substring at index zero. For this, all you have to do is replaced zero with two in the initial template and you will get the corresponding value I printed out twice inside the string. So let's try this. Instead of 01 and two as we had initially now we have two, one and two, that's repeating club dot four in our string. Let's test this. Nice, just what we wanted.

Now it's time to wrap up things with strings by learning about string slices.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.