Python Learning Chapter 2nd

Source: Internet
Author: User
Tags string format

1. String:

Create a string in Python we can use quotation marks ' or '.

Python accesses substrings, you can use square brackets to intercept strings:

var= "Hello world!"

Var2= "Runover"

Print ("var[0]:", var[0])

Print ("Var2[1:5]:")

The above code performs as follows:



Of course Python can also update the string:

var above

Print ("Updated string:", VAR[:6]+VAR2)

Execute the code as follows:

Updated string: Hello Runover, thus we can see that the string can be intercepted.

2. Operators:

Python is easy for other languages, one of which is the string operator:

As String A=java B=python, there are

+ Character channeling Link: a+b output is Javapython

* Duplicate output string: A*2 output is also Javajava

[] Get the characters in a string by index: a[1] The output is E

[:] Intercepting part of a string: A[1:4] Output is AVA

In member operator (if the string contains the given character returns true): The output of H in a is 1 (1 is true,0 to flase)

The not in transverse is obviously the last inverse;

% format string: This is more complicated, we'll explain in more detail below

The most basic usage of% is to insert a value into a character that has the string format character%s:

Print ("My name%s this year%d years"% (' Ti Xiaodong ', 20)) The output is also my name Ti Xiaodong this year 20 years old

% and multiple letter combinations have different effects such as the following:

%c: Formatting characters and their ASC11

%d: Formatted integer

%u: Formatting unsigned integers

%o: Formatting octal

%x: Format 16-bit

%f: Formatting floating point

%p: Using 16-bit binary format variable address


Python Learning Chapter 2nd

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.