python0.4----String

Source: Internet
Author: User

Tag:%s You error val byte string operation python expression number

string : Text enclosed in single or double quotation marks (without quotation marks)

String manipulation:

String Connection: STR1=STR2+STR3

Output repeating string: STR1=STR2 * 3

to access a character in a string :

use subscript to access the I character: string name [subscript i], A string containing text can also, a literal equivalent to a character, although it may occupy 2-4 bytes.

The string is immutable:

Once a string is placed in memory, the string in that memory cannot be changed, and the number type is the same.

For example, to modify a single character in a string str1. Str1= ' yuliangiloveyou '. Because the string cannot be changed, the expression str1[1]= ' a ' is wrong and cannot be executed successfully.

If you want to change a string variable, you can only modify the reference.

For example: str1= ' yuliangiloveyou '. ID (str1) = 94618624. Use the str1= ' Iloveyouyuliang ' statement. ID (str1) = 94619456. Since the two-time ID is different, the subsequent assignment statement changes the string reference rather than the string inside the memory.

The process of saving strings to memory:

For example: ' A '

A through the ASCII code table-"Decimal number-" binary data

To intercept a part of a string:

Str2[[start,] [: stop]], intercepts the characters in the [start,stop] interval of the string str2, and prints str2 the entire string if start and stop are all omitted.

Determine if the str1 is in str2:

STR1 in str2: If STR1 is a substring of str2, 1 is returned, otherwise 0 is returned.



Formatted output:

%d,%s,%f: Formatting placeholder advantages: Easy to print multiple data

Print ('%s%d%.3f '% (str1, NUM1,FLOAT1)) Benefits: Easy to print multiple data

\: Escape character ( converts some characters to characters with special meanings)

\ n denotes a character, and the function is a newline

\ \: Outputs a \

\ ': Output a '

\ ": Output One"

\ t: Tab (Windows defaults to 4 spaces, and Linux defaults to 8 spaces).

If there is a lot of line breaks inside The string, write affects reading. Can be in the following form:

Print ("" "goog

dddd

RER """)

If there are many characters in the string that need to be escaped, you need a lot of \, in order to simplify, Python allows the use of r to indicate that internal strings are not escaped by default (Windows uses \ To connect each directory, and Linux uses/connects directories).

Print (R ' \q\w\r\t\y\u\\gfg\dfasd\ "\FASD")

Functions related to Strings:

Eval (str): evaluates the string str as a valid expression and returns the result of the calculation. and cast int () function type, but stronger with int ()

Example: Num1=eval ("123")------>num1=123 like int ()

Num1=eval (' 12+3 ')----->num2=5 is stronger than int ()

Len (str): Returns the length of the string.

Str.lower (): Converts the uppercase letters in STR to lowercase letters. STR itself does not change, because a string variable is a variable that cannot be changed, it simply re-opens up a memory space and places the converted string in the new memory space.

Str.upper (): Converts the uppercase letters in STR to lowercase letters ...... ..... ............... (Other ibid.).

Str.swapcase (): Lowercase to uppercase, uppercase letters in lowercase ....... ..... ............. (Other ibid.).

Str.capitalize (): Capitalize first letter.

Str.title (): Capitalize the first letter of each word inside the string.

Str.center (Width,fillchar): Sets the length of the string to width, centers the str string, and fills the other position with Fillchar, returning the new string.

Str.ljust (Width[,fillchar]): Sets the length of the string to width, aligns the STR string to the left, and other locations with Fillchar padding, returning the new string.

Str.rjust (Width[,fillchar]): Sets the length of the string to width, aligns the STR string to the right, and other locations with Fillchar padding, returning the new string.

Str.zfill (width): Sets the length of the string to width, right, and other positions to fill with 0 characters.

Str.count (Str1[,start][,end]): Returns the number of occurrences of str1 in Str, the search range is [start,end], and the default search scope is [0,len (str)]

Str.find (Str1[,start][,end]): in [Start,end) from left to right to find whether STR1 is included in Str, if the STR1 is included in Str, return the first to find the subscript, if the str1 does not exist, then return 1

Str.rfind (Str1[,start][,end]): in [Start,end) from right to left to find whether STR1 is included in Str, if the STR1 is included in Str, return the first to find the subscript, if the str1 does not exist, then return 1

Str.index (Str1,start=0,end=len (str)): As with the Find method, an exception is generated if the str1 does not exist ValueError

Str.rindex (Str1,start=0,end=len (str)): As with the RFind method, an exception is generated if the str1 does not exist ValueError

Str.lstrip ([char]): Delete the character char specified on the left of STR and remove the space by default

Str.rstrip ([char]): Delete the character char specified on the right of STR and remove the space by default

Str.strip ([char]): Delete the character Char to the left and right of STR and remove the space by default

python0.4----String

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: info-contact@alibabacloud.com 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.