Teen python Series 10. Strings

Teenager python Series Directory - Old Programmer's Blog - CSDN Blog

Teenager python teaching video ppt source code

Introduction

In Python, the text enclosed in quotation marks is a string, and the string can also be saved in a variable for use. If the text in the string contains double quotes, you can enclose the string in single quotes, print("You're such a 'good guy'"). If the text in the string contains single quotes, you can enclose the string in double quotes; print('You are such a "good guy"!'). In Python, although you can mix single quotation marks ("), double quotation marks (""), and triple quotation marks (""), it is better not to mix them when unnecessary. After all, simplicity is beauty!

escape character

Escape characters are part of the formal grammars of many programming languages, data formats, and communication protocols. Just like the code word between the programmer and the Python interpreter, all escape characters consist of a backslash (\) followed by a letter, such as "\t" for horizontal tab; "n" for newline

print ("The bright moonlight in front of the bed\nSuspected to be frost on the ground\nLooking up at the bright moon\nLooking down and thinking about hometown")

judge

find and replace

split and join

case conversion

How to remove blanks

code practice

​
Remove spaces and special symbols

s.strip().lstrip().rstrip(',')

copy string

#strcpy(sStr1,sStr2)
sStr1 = 'strcpy'
sStr2 = sStr1
sStr1 = 'strcpy2'
print sStr2

connection string

#strcat(sStr1,sStr2)
sStr1 = 'strcat'
sStr2 = 'append'
sStr1 += sStr2
print sStr1

find character

#strchr(sStr1,sStr2)
# < 0 is not found
sStr1 = 'strchr'
sStr2 = 's'
nPos = sStr1.index(sStr2)
print nPos

compare strings

#strcmp(sStr1,sStr2)
sStr1 = 'strchr'
sStr2 = 'strch'
print cmp(sStr1,sStr2)

Scan a string to see if it contains the specified characters

#strspn(sStr1,sStr2)
sStr1 = '12345678'
sStr2 = '456'
#sStr1 and chars both in sStr1 and sStr2
print len(sStr1 and sStr2)

string length

#strlen(sStr1)
sStr1 = 'strlen'
print len(sStr1)

convert case in string

#strlwr(sStr1)
sStr1 = 'JCstrlwr'
sStr1 = sStr1.upper()
#sStr1 = sStr1.lower()
print sStr1

Appends a string of specified length

#strncat(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = 'abcdef'
n = 3
sStr1 += sStr2[0:n]
print sStr1

String specified length comparison

#strncmp(sStr1,sStr2,n)
sStr1 = '12345'
sStr2 = '123bc'
n = 3
print cmp(sStr1[0:n],sStr2[0:n])

Copy characters of specified length

#strncpy(sStr1,sStr2,n)
sStr1 = ''
sStr2 = '12345'
n = 3
sStr1 = sStr2[0:n]
print sStr1

put the string before n characters are replaced with the specified characters

#strnset(sStr1,ch,n)
sStr1 = '12345'
ch = 'r'
n = 3
sStr1 = n * ch + sStr1[3:]
print sStr1

scan string

#strpbrk(sStr1,sStr2)
sStr1 = 'cekjgdklab'
sStr2 = 'gka'
nPos = -1
for c in sStr1:
    if c in sStr2:
        nPos = sStr1.index(c)
        break
print nPos

flip the string

#strrev(sStr1)
sStr1 = 'abcdefg'
sStr1 = sStr1[::-1]
print sStr1

find string

#strstr(sStr1,sStr2)
sStr1 = 'abcdefg'
sStr2 = 'cde'
print sStr1.find(sStr2)

split string

#strtok(sStr1,sStr2)
sStr1 = 'ab,cde,fgh,ijk'
sStr2 = ','
sStr1 = sStr1[sStr1.find(sStr2) + 1:]
print sStr1
#or
s = 'ab,cde,fgh,ijk'
print(s.split(','))

connection string

delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']
print delimiter.join(mylist)

Show only letters and numbers
def OnlyCharNum(s,oth=''):
    s2 = s.lower();
    fomart = 'abcdefghijklmnopqrstuvwxyz0123456789'
    for c in s2:
        if not c in fomart:
            s = s.replace(c,'');
    return s;
 
print(OnlyStr("a000 aa-b"))


intercept string

str = '0123456789'

print(str[0:3])     #Truncate the first to third characters

print(str[:])       #Truncate all characters of a string

print(str[6:])      #Truncate the seventh character to the end

print(str[:-3])     #Truncate from the beginning to before the third-to-last character

print(str[2])       #Truncate the third character

print(str[-1])      #Truncate the penultimate character

print(str[::-1])    #Creates a string in the reverse order of the original string

print(str[-3:-1])   #Truncate the characters before the third to last and the first to last

print(str[-3:])     #Truncate the third-to-last digit to the end

print(str[:-5:-3])  #Reverse order interception
​

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

The csdn article recommends the affected solution 10 words and 10 lines

Tags: Python numpy programming language

Posted by squallm1 on Sun, 09 Oct 2022 22:43:30 +0300