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

Python variable

編輯:Python

1 Version view

C:\Users\user>python --version
Python 3.10.1
C:\Users\user>python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello,phython!")
Hello,phython!

2 Variable

2.1 character ( strand ) Type variable

2.1.1 character ( strand ) Type variable

message='hello,This is my first code.' # What is quoted in single or double quotation marks
print(message)

Running results :

hello,This is my first code.

2.1.2 String manipulation

(1) Case write

name='josome liu'
print(name.upper()) # Capitalize all
print(name.lower())# All to lowercase
print(name.title())# The first character is capitalized 

Running results :

JOSOME LIU
josome liu
Josome Liu

(2) String merge

first_name='liu'
last_name='josome'
full_name=f'{first_name} {last_name}'# Add a space in the middle
print(full_name.title()) # title case
full_name2=first_name+last_name
print(full_name2)

Running results :

Liu Josome

liujosome

(3) String slice

string = "Line1-abcdef Line2-abc Line3-ab Line4-abcd"
string_find='ine1'
print(string)
print(string.split())# Slice all the words
print(string.split(' ', 1))# Slice only one
print(string.find(string_find,0,10))# from 0( Left ) To 10( Right ) Find the characters in string_find The location of , No return found -1

Running results :

Line1-abcdef Line2-abc Line3-ab Line4-abcd
['Line1-abcdef', 'Line2-abc', 'Line3-ab', 'Line4-abcd']
['Line1-abcdef', 'Line2-abc Line3-ab Line4-abcd']
1

2.1.3 blank ( Space , tabs , A newline )

(1) Through tabs (\t) Or line breaks (\n) Add blanks .

message_hello='\n\tHello,\nmy\tgirl!'
print(message_hello)

Running results :


”    Hello,
my    girl!“

(2) Delete blanks

message_hello=' Hello '
print(message_hello)
print(message_hello.lstrip())# Delete the left margin
print(message_hello.rstrip())# Delete the right margin
print(message_hello.strip())# Delete the blanks on both sides
print(message_hello.count('l'))# Count the number of occurrences of characters 

Running results

”Hello  “
”  Hello“
”Hello“

2

2.1.4 Python Three quotes (triple quotes)

Ensure that if the string contains escape characters, such as \n,\t," etc. , It still shows two days HTML String in , namely What you see is what you get .

errHTML = '''
<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>ERROR</H3>
<B>%s</B><P>
<FORM><INPUT TYPE=button VALUE=Back
ONCLICK="window.history.back()"></FORM>
</BODY></HTML>
'''
print(errHTML)

Running results

<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>ERROR</H3>
<B>%s</B><P>
<FORM><INPUT TYPE=button VALUE=Back
ONCLICK="window.history.back()"></FORM>
</BODY></HTML>

Without double quotation marks : The compiler will recognize errors

For more operation reference of characters :

Python character string - Python course - Self improvement school Python character string String is the most Python Total common data types . We can use quotation marks to create strings . Creating a string is simple , Just assign a value to the variable . for example : var1 = 'Hello World!' var2 = 'Python Programming' Python Access the value in the string Python Single character type... Is not supported , Single characters are also Python Also used as a string . Python Access substring , have access to ..https://code.ziqiangxuetang.com/python/python-strings.html

2.1.5 character ASCII

print(ord('C'))# Show ASCII code
print(ord(' people '))# The character integer represents
print(chr(67))#ACSCII Convert to character
print(chr(20154))#ACSCII Convert to character 

Running results :

67
20154
C
people


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