preface

Ha ha ha, yesterday is June 1 children’s Day? So as more than 20 years old I, and lazy not to update, the Children’s Day to go. After all, I am also a child with a childlike innocence, so you do not urge me to update in the background, let me a little lazy! So get back to business, believe that by learning from last class, the students have mastered the python variables and strings of simple introduction to the definition of learning, so in this lesson is to learn the string of in-depth study, suggest all the students here, really want to practice more, learning to program, read to learn, don’t always understand, and don’t practice, It’s not going to work when it comes to code.

String formatting (often used to concatenate urls, paths, database fields, etc.)

Python provides “%” to format the output of various types of data, for example:

price = 108
print("the book's price is %s"% price) : the book's price is 108
Copy the code

So what do you do when you have multiple values?

user = "Virus"
age = 8
print("%s is a %s years old boy"% (user, age)is a 8 years old boy
Copy the code

Other conversion specifiers

Case-dependent method

Title () : Caps the first letter of each word Lower () : caps the entire string upper() : caps the entire string

Example: the title ()

a = 'our domain is crazyit.org'
printOutput: Our Domain Is Crazyit.OrgCopy the code

lower()

a = 'Our Domain is Crazyit.org'
print(a.power ()) Output: our domainis crazyit.org
Copy the code

upper()

a = 'our domain is crazyit.org'
print(a.uppper ()) Output: OUR DOMAIN IS CRAZYIT.ORGCopy the code

Delete the blank

Strip () : removes the whitespace before and after a string. Lstrip () : removes the whitespace before (left) and rstrip() : removes the whitespace after (right)

Example: strip ()

s = ' this is a virus '
print(s.trip ()) Output: thisis a virus
Copy the code

lstrip()

s = ' this is a virus '
print(s.lstrip()) Output: thisis a virus
Copy the code

rstrip()

s = ' this is a virus '
print(s.rstrip()) Output: thisis a virus
Copy the code

Find and replace

Endswith () : determines whether the string begins with a specified substring. Find () : finds the position of the specified substring in the string. If the specified substring is not found, returns -1 index() : Looks for the position in the string where the self-stator string appears. If the specified substring is not found, raises ValueError error Replace () : Replaces the target substring in the string with the specified substring

Example: the startswith ()

s = 'virus is good man'
print(s.startswith('virus') Output result:True
Copy the code

endswith()

s = 'virus is good man'
print(s.endswith('man') Output result:True
Copy the code

find()

s = 'virus is good man'
print(s.find('good') Output result:9
Copy the code

index()

s = 'virus is good man'
print(s.index('man') Output result:14
Copy the code

replace()

s = 'virus is good man'
print(s.replace('man'.'women') Output result: virusis good women
Copy the code

Split, join

Split () : splits a string into phrases according to the specified separator join() : joins phrases into a string

Ex. :

split()

s = 'crazyit.org is a good site'
print(s.split('. '))
# Note: if split() does not pass a delimiter, it is equivalent to a space splitOutput: ['crazyit'.'org is a good site']
Copy the code

join()

s = 'I am virus'
mylist = s.split()
print('/'.join(myList)) Output: I/ AM /virusCopy the code

Serial correlation

The first thing to understand is what is a sequence? Strings are essentially composed of multiple characters, so programs allow manipulation of characters by index, such as getting the character at the specified index, getting the position of the specified character in the string, and so on. A Python string is indexed directly in square brackets ([]). The first character in the string has an index of 0, the second character has an index of 1, and so on. In addition, Python allows indexes to be computed from the end, with the last character having an index of -1 and the penultimate character having an index of -2…………… And so on.

Is not to see a long paragraph, feel a little meng, I just started to look at the time also feel meng, what is this thing? Do you have such a question, let me briefly tell you what it means

s = 'IamVirus'I a m V I R u S0 1 2 3 4 5 6 7(from right to left) I a m V I r u s -8 -7 -6 -5 -4 -3 -2 -1
Copy the code

Each string has a subscript that starts at 0 if you’re counting from the left, minus 1 if you’re counting from the right, and so on

Example: Take a single

s = 'IamVirus'
print(s[1]) ACopy the code

Take a

s = 'IamVirus'
print(s[1:4]) output amVCopy the code

If it’s negative

s = 'IamVirus'
print(s[1: -2]) Output: amVirCopy the code

Students extracurricular problems

1. Why is there no video tutorial? A: There will be a video tutorial in the future, but I haven’t had time to record it yet. I will record it when I am free.

2. Why not write everything at once instead of updating it day by day? A: I could have written it all at once, but Python is updated every day, so I’m constantly updating it every day, and I think there’s a lot of content to write the tutorial all at once and you won’t be able to master it, so just follow my rhythm!

If you have any questions, please contact me in the background! I will give you one by one of the answers, the same, what problems encountered in learning can also find me to help you solve oh!!

conclusion

Well, that’s the end of the class, you have to study hard, because I code word is also very tired, hopefully watching my tutorial, you will become more and more interested in Python! We will give you some practice questions in the next class. Stay tuned!