Lecture 14) Other problems with text and strings

Python 3: A Problem-Solution Guide Section 2) Problems and their solutions in python 3
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
Already have an account? Log In

Transcript

In this section, I'm going to address more problems related to text. The first problem here is to find the location of a substring within a string object. In order to solve this problem, we need to use find method. In this example, I have a variable called my str, which has data assigned to it, and it's in a string format. It says he's my friend, if you are going to search for a substring, let's say is in that variable, you need to use find function. So you write down my str dot find open close parentheses and the string that you want to find.

This will return a number and that number is the index number of the location of that text within the string object. On the other hand, if you are going to search for a substring, that doesn't exist, Python returns minus one to notify you that that string does not exist to get a substring by slicing it. You need to slice method. So this problem here is about slicing out a substring. From a string object, we have a variable here called str one, and we assigned HelloWorld as a datum to it. to slice out the first word, which is Hello, we need to slice out the first six characters, including the space.

To do so use print function, open, close parentheses, and feed the variable to the print function, and open close brackets. In between the brackets supply the index number of H, which is the first character and this space, which is the sixth character and separate them by colon, close brackets and close parentheses. This will return the Hello which is the first word that we want it to slide out from the Hello World. Sometimes you need to replace a portion of a string with a new text. The solution could be by slicing out the unwanted quote Replace it with your text using concatenation and string slicing technique. So here we have a data sheet are good and assign the data to my text variable.

We want to slice out the text R and replace it with its. So what we do here, we first print it out to see, what does it contain? It contains, she argued this is not mandatory. You correct this, we declare another variable, I named it as correction, and it's equal to your variable with the sliced parts. The first part and the last part are correct. The middle text, or the part in between is not correct.

In order to slice out the first word, we supply my text variable and slice out the first word, which spans from the first character to the fourth character. This slice includes s to the space character, which is she Seven character to the end includes good was useless add these two parts you can concatenate them with the term is. And when you print out correction, it shows you she is good, which is the correct statement, there is another method of doing the same thing, which is by using replace function. So you declare a variable, let's say correct var, you say this is equal to the old variable, Doctor replace, open close parentheses, then you supply the old string, which is the one that you don't want to appear comma the new string, which is the one that you want to appear and account number. The count is a number that means the number of times this change should happen.

So when you run this, it says she is a string reversal is another process that you may need to reverse a string in order to study the string in a reversed fashion. This could be important in instances where you want to study palindromic texts, the sentence Such process includes open close brackets and in between the brackets you use colon colon minus one. This will reverse any string that has been supplied to it. Here we have a variable called my txt, and it's equal to a text that says python programming. If you want to reverse this text, you write down my txt, open close brackets. These are square brackets and colon colon minus one.

You can print this out by writing down print, open, close parentheses, and between the parentheses write down the variable that contains this data and it will immediately show you the reverse string. Sometimes you need to remove white spaces from the ends of your text containing variables. In order to do so you need to use strip function. Here we have two variables text one and texture text one contains spaces from both ends. Text two doesn't have any spaces. If you use strip function, it will remove spaces from the ends of the first variable as we use the strip function.

For the first variable, we also concatenate these two variables in order to show how the spaces are removed from the ends of the first variable. If you want to change the text case, you could either choose uppercase or lowercase in order to convert a text to uppercase use dot upper method. And if you want to convert the text to lowercase use dot lower method. Here we have a variable called txt and it contains the data hello world. In order to change the Hello World text to uppercase, use dot upper so we write down prints open close parentheses, txt dot upper open, close parentheses and close parentheses and I will show you the HelloWorld text in a uppercase fashion. To convert text to lowercase here, we print out txt dot lower open close parentheses and close parentheses and it will show you the Hello World in lowercase.

Sometimes you need to iterate over the characters of a string. Here we have a variable called txt and it contains HelloWorld like the previous one you can use for loop in order to iterate we just print them The elements here and we are interested in showing each cat X ray with its corresponding index, you can do so by writing down for i in enumerate. txt colon print, i comma n equals to empty string. This will show you the characters next to each other and their corresponding indices. If you want to show this one after the other, you can do so by excluding the end statement inside the print function to iterate to perform statistical analysis. On strings, you need to have a paragraph.

The first statistical procedure is count you can count the occurrence of a character or a word to do so use count function. So here we have my txt as a variable. to count a word let's say Peter in this directory to text, you write down my txt dot count, open close parentheses and between the parentheses you write down the word Peter, you can return The length of your data containing variable and it will show you the number of characters here, min and max show you the minimum and maximum values of your text. Here the carriage return is minimum value and the maximum value is a comma, you can perform word based statistics on the same text. However, you need to change the text from characters to words you need to use split function. So you write down my txt dot split open close parentheses and press enter and it will become a list and the list contains words.

You can assign this to a variable txt one and then count the number of occurrences of PETA in it you can see that this is the same because the term Peter was followed by a space and I separated it from the other words, you can also perform other types of statistical analysis onwards. So if you want to retrieve the length of The text in terms of words, I will show 34. So you print it out and you, you write down print, land on Close parentheses and the variable containing the words, it will show you 34 because there are 34 words, you can also perform other kind of statistics like the ratio of the word Peter on the total number of words in the text one variable. And as you can see here, it returns not point 11, which is 11%. So you can see that 11% of the words was composed of the word beater.

So this was about problems related to strings. In the next coming sections. I'm going to address problems related to numbers.

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.