Python basic (string) day two

1. Single quotation mark and escape Guide

2. Splicing string

3. Format string

4. Common methods

#Remove spaces and special symbols

#Search and replace strings

#String testing and replacement functions

#Segmentation of strings

#string module

#To create a string, you can use double quotes(" ")Or single quotes(' ')To create
str1 = "hello"
str2 = 'python'
print(str1)
print(type(str1))#See str1 Types

#String splicing, will str1 and str2 Concatenate into a new string str3
#Mode 1: use+To stitching together
str3 = str1 + str2
print(str3)
print(type(str3))

#The second way"join"To link strings
str1 = "hello"
str2 = "python"
str3 = ','.join(str1 + str2)
print(str3)
print(type(str3))

#If you want to print()Method to display multiple strings. You can use "," to split multiple strings
a = "good"
b = "good"
c = "Study"
print(a,b,c,"Make progress every day")

#print()Method will wrap after printing. In fact, he has a end Parameters, you can use"end="To remove line breaks
str1 = "hello"
str2 = "python"
str3 = "My name is black"
print(------Gorgeous dividing line------)
print(str1, end='')
print(str2, end='')
print(str3)

#print When printing characters in multiple lines, the default is to separate them with a space. We can use sep To specify the split symbol
name = "RCS"
print("hello", name, sep='*-*')

#python Recommended later.format()To format a string
#The first bracket accepts 1, the second 2, and the third (1+2)
str1 = '{}+{}={}' .format(1,2,1 + 2)
print(str1)

#If there is no data in parentheses, the default is to fill it from left to right
#If there is mathematics in the brackets, it will be filled once according to the number position, here{1} = 2 {0} = 10
{2} = 2/10
#python Like other languages, it's starting from scratch, not 1
str1 = '{}/{}={}' .format(10,2,10/2)
str2 = '{1}/{0}={2}' .format(10,2,2/10)
print(str1)
print(str2) 

#Alignment in formatting
#<Align left no effect here .3f Represents to three decimal places
str1 = '{n1:<}/{n2:<}={n3:.3f}' .format(n1=10,n2=2,n3 =10 / 2)
print(str1)

#Remove spaces and line breaks(/r) .strip()Method
name = "        Study hard, study every day"
print("Before transformation:", name)
name = name.strip()
print("After transformation:", name)

#Remove a character
name = "Study hard"
print("Before transformation:",name)
name = name.strip("ah")
print("After transformation:",name)

#Remove left space
name = "          Study hard"
print("Before transformation:",name)
name = name.lstrip()
print("After transformation:",name)

#Remove spaces and line breaks on the right
name = "study hard  "
print("Before transformation:", name)
name = name.rstrip()
print("After transformation:", name)

#Find the number of times a character appears in a string
str1 = "black python"
n = str1.count('n')#.count View the number of occurrences
print('n There are:', n, end='second')#end As mentioned earlier, specify what to end the output with

#Capitalize first
name = black
print("Before transformation:", name)
name = name.capitalize()#.capitalize Function is to capitalize the first letter
print('After transformation:', name)

# Put the string in the middle and use it on both sides'-'Make up
name = "Python Learning group"
print("Before transformation:", name)
name = name.center(20, '-') # 20 Refers to the length of the transformed string .conter Stands for putting characters in the middle'-'Can be replaced at will
print("After transformation:", name)

#Find the position of the target character in the string, and return the first position when there are more than one
#Can't find it back-1
name = "python Study"
i = name.find('learn')#.find Query statement
temp = '{}in{}First time in{}Position'.format(name,'learn',i)#Note that all symbols must be in English,
Otherwise, an error will be reported .format As mentioned earlier, it's a format string, which is equivalent to typesetting. One more thing to note, starting from zero
print(temp)

 

 

#String substitution
name = "I am learning. java"
print("Before transformation:", name)
name = name.replace('java', 'Python')
print("After transformation:", name)

#See if the strings are all letters or text and have at least one character
name = 'abcdef'
name2 = 'python2 Study'
print(name.isalpha())#isalpha A function: judging characters ch English letters or not
print(name2.isalpha())

#See if it's all numbers
name = '12345'
name2 = 'abc123'
print(name.isdigit())
print(name2.isdigit())#isdigit Function to determine whether it is a number

#See if they are all lowercase islower() Capitalization issupper()
name = 'abc123'
print(name.islower())
print(name.supper())

#Character divider
 word = "thousands of trees and flowers bloom all night, and the sky and the earth are white. "
Su'e's waist is dancing, and Bai Yutang's deep music is urging. "
The scholar of the urn is threatening the policy, and the armour is holding the title. "
Your bones are thin and want to be clear. I always have a glass of wine. "
wordList = word.split('.') to generate a list
print(wordlist)

Tags: Python Java

Posted on Mon, 02 Dec 2019 07:02:54 -0500 by pjkinann