Python: String

Source: Internet
Author: User

#!/usr/bin/python3#stringvar1='Hello world!'var2="ABCDEFG"Print("var1:"+var1)Print("var2:"+var2)Print("Var1[0]:"+var1[0])Print("var2[2:]:"+var2[2:])Print("Var2[2:3]:"+var2[2:3])Print("var1:"+var1*2)#Transfer character"""\ \ backslash symbol \ ' single quotation mark \a bell \b backspace \ n newline \ tab \ r Enter"""#python character operator"""+ String Connection * Duplicate output string [] Index gets part of the string [:] intercepts a part of a string in characters if the given character is not in% formatted string"""#python formatting symbols#string Formatting"""%s formatted string%c formatted character%d formatted integer%f formatted floating-point number, can be specified in bits%r universal format character, direct output"""Pi=3.1415926str1="Good Boy"Print("pi=%.2f"%pi)Print("str1%s"%str1)#built-in functionsStr2="abcdefGHabcd123456789"#S.count (sub[, start[, end]), intPrint("String%s"%str2)Print("string Length:", Len (str2))Print("returns the number of occurrences count (def):%d"% (Str2.count ("ABCD")))#S.endswith (suffix[, start[, end]), BOOLPrint("whether to end with a character endswith (def):%r"% (Str2.endswith ("ABCD")))#find not found back-1#S.index (sub[, start[, end]), int and find, but no error is foundPrint("Find", Str2.find ("BCD", 0,len (str2)))#rfind not found. Return-1Print("RFind", Str2.rfind ("BCD", 0,len (str2)))#s.isalnum (), bool at least one character and all consist of characters and numbers (special symbols return false, such as.)Print("whether it is all by numbers and characters Str2.isalnum ()", Str2.isalnum ())#S.isalpha (), BOOL has at least one character and is all charactersPrint("are all composed of characters Str2.isalpha ()", Str2.isalpha ())#s.islower (), bool with a letter and all lowercase to return truePrint("whether the characters are all lowercase str2.islower ()", Str2.islower ())#s.isupper (), bool with a letter and all uppercase to return TruePrint("whether the characters are all uppercase Str2.isupper ()", Str2.isupper ())##S. IsDigit () bool is all made up of numbersPrint("is all made up of numbers str2.isdigit ()", Str2.isdigit ())Print("is all made up of numbers str2.isnumeric ()", Str2.isnumeric ())Print("is only a space str2.isspace ()", Str2.isspace ())#Lower () Upper ( )Print("Convert all to lowercase str2.lower ()", Str2.lower ())Print("convert all to uppercase Str2.upper ()", Str2.upper ())Print("Turn uppercase and lowercase characters into uppercase", Str2.swapcase ())

Python: String

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.