程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics_ Basic operation of string

編輯:Python
# Operator
# Operator of + Law
num1 = 10
num2 = 52
res = num1 + num2 # Give priority to the one on the right
print(res)
# Operator - Law
res2 = num1 - num2
print(res2)
# Operator * Law
res3 = num1 * num2
print(res3)
# Operator / Law
res4 = num1 / num2
print(res4)
print(100/5)
# ZeroDivisionError Error type
# print(100/0) # The divisor cannot be zero 0, Will report a mistake
# Operator % Law
res5 = num1 % num2 # % Meaning of remainder
print(res5)
# Operator priority
res6 = (num1 * num2) + (num1 + num2 )
print(res6)
print('************************************')
# The assignment operation : Value to variable
# assignment += Use Add is equal to ( Cumulative assignment )
# chocolate 120 Crayfish 80 red wine 200
# Check out at the cashier sum = 0
sum = 0 # Variable assignment
# chocolate 120
# sum = sum + 120
sum += 120 # Cumulative assignment
print(sum)
# Crayfish 80
sum += 80
print(sum)
# red wine 200
sum += 200
print(sum)
# assignment -= Use Minus is equal to ( Subtractive assignment )
# On the original basis , Subtract 200
sum -= 200 # Subtractive assignment
# sum = sum - 200
print(sum)
print(' The money I will eventually have to pay is :',sum)
print('*****************************************')
# Comparison operator
# ==( be equal to ) !=( It's not equal to )
# >( Greater than ) >=( Greater than or equal to )
# <( Less than ) <=( Less than or equal to )
# The result of their comparison is : Boolean value (False,True)
my_mooney = 2000
you_money = 520
# Whether it is equal or not
print(my_mooney == you_money) # fake (False)
# Is it unequal
print(my_mooney != you_money) # Really? (True)
# Greater than
print(my_mooney > you_money) # Really? (True)
# Greater than or equal to
print(my_mooney >= you_money) # Really? (True)
# Less than
print(my_mooney < you_money) # fake (False)
# Less than or equal to
print(my_mooney <= you_money) # fake (False)
print('*****************************************')
# Logical operations
# and( And ) or( or ) not( Not )
# The logical result is : Boolean value (False,True)
# input() Accept user input The data type of the string Is a function
# and( And )
# age = (input(' Enter your age :'))
# height = (input(' Enter your height :'))
# wage = (input(' Enter your salary :'))
# Input condition
# String number , Need to be converted to int type ,int( String in numeric form )
# The first requirement is
# res = (int(age) > 20) and (int(height) > 175) and (int(wage) > 10000) # and All conditions need to be met
# print(res) # Print
# or( or )
# The second requirement
# res = ((int(age) > 20) or (int(height) > 175)) and (int(wage) > 10000) # or Only one condition needs to be satisfied
# print(res) # Print
# not( Not )
# Non operation - Take the opposite - Sing the opposite
# resa = (not int(age) > 20) # Input greater than 20 Value ,not Then it's fake
# print(resa)
print('*****************************************')
# String manipulation
# An empty string
s = "" # character string
s1 = None # Does not represent any data type .
# print(s)
# print(s1)
# print(type(s1))
# In pairs . Start with double quotation marks , Must end in double quotation marks . Start with single quotation marks , Must end in a single quotation mark .
# person_info = ' I'm Miss Jane , I like "python30 period ", I have a holiday with you today !'
# print(person_info)
#
# # Escape of string :\ As the data content of the long gauge , Not regard as python Special treatment .
# person_info = ' I'm Miss Jane , I like \'python30 period \', I have a holiday with you today !'
# character string : It's an immutable data type . Read only cannot modify the string itself .
# Numbers 、 Boolean value It's all immutable .
person_info = ' I'm Miss Jane , I like "python30 period ", I have a holiday with you today !'
# Take the value of a certain position
# from 0 Start . positive ,+1 reverse -1 Variable name [ Indexes ]
print(person_info[6])
print(person_info[-1])
# Interval values - Substring The starting point The end point step
# from 0 Start , To the subscript 6 Variable name [ rise : end ]
# If the step size is positive , Indicates forward slice . from 0 Start
# If the step size is negative , Indicates a negative slice . sleep
print(person_info[0:6]) # 0,1,2,3,4,5 step :1
print(person_info[:7]) # 0,1,2,3,4,5,6
print(person_info[:7:2]) # 0,2,4,6 In steps of 2
# print(person_info[7:1:-2]) # 7,5,3 In steps of 2
print(person_info[-1:-10:-1]) # -1,-2,-3,-4,-5,-6,-7,-8,-9
print(person_info[:])
print(person_info[::-1]) # interview
print("****************** Common methods / function *******************")
# find( Substring ) Forward lookup substring , The returned values found are >=0. I can't find it -1
index = person_info.find(" I'm Miss Jane ") # 0 Forward lookup substring .
print(index)
# count( character / character string ) Count the number of occurrences in the original string
count = person_info.count(" I ")
print(count)
# len( character string ) Gets the total length of the string
print(len(person_info))
# upper() Converts the letters of a string to uppercase . Regenerate a string . It doesn't change the original string .
res = person_info.upper()
print(res)
print(person_info)
# lower() Converts the letters of a string to lowercase . Regenerate a string . It doesn't change the original string .
price_info = 'WTO It is an international organization in the world '
res1 = price_info.lower()
print(res1)
# Homework after class
# 1、 Now there are strings :str1 = 'python cainiao 666'
# 1、 Please find the number 5 Characters .
str1 = 'python cainiao 666'
print(str1[4])
# 2、 Please copy a string , Save as str_two( Use assignment )
str_two = 'python Study 666'
print(str_two)
# 3、 Please find the middle character .( The string length is even .)
print(len(str1))
print(str1[8:10])
# 2、 Orange calculator : Write a piece of code , Prompt the user to enter the price of orange , And weight , Finally calculate the amount that should be paid !( No need to verify data , Just pass in numbers .)
# ( Use input Method to get user input )
price = (int(input(' Please enter the orange price :')))
weight = (int(input(' Please input the orange weight :')))
luck = price * weight
print(' The total amount you need to pay is :',luck)
#
#
# 3. Drill through string operations
my_hobby = "Never stop learning!"
#
# Interception from Location 2 ~ Location 6 String
print(len(my_hobby))
print(my_hobby[1:6])
print(my_hobby[1:])
# Interception from Location 2 ~ At the end of String
print(my_hobby[1:20])
# Interception from Starting position ~ Location 6 String
print(my_hobby[:6])
# Intercepts the complete string
print(my_hobby[:])
# from Indexes 3 Start , Every time 2 Take one character out of the characters
print(my_hobby[3::2])
# Intercepts two characters at the end of the string
print(my_hobby[-1:-3:-1])
# Reverse order of strings
print(my_hobby[::-1])
# explain :“ Location ” It refers to the position of the character ( For example, location 1, Refers to the first character “N”),“ Indexes ” Refers to the index value of the character ( Such as the index 0, Represents the first character “N”)

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved