learning python string

2010-08-22  来源:本站原创  分类:Python  人气:128 

# Python string operations

'''1. Copy the string'''

# Strcpy (sStr1, sStr2)

sStr1 = 'strcpy'

sStr2 = sStr1

sStr1 = 'strcpy2'

print sStr2

'''2. Connection string'''

# Strcat (sStr1, sStr2)

sStr1 = 'strcat'

sStr2 = 'append'

sStr1 + = sStr2

print sStr1

'3''. Look character'''

# Strchr (sStr1, sStr2)

sStr1 = 'strchr'

sStr2 = 'r'

nPos = sStr1.index (sStr2)

print nPos

'4''. Comparative string'''

# Strcmp (sStr1, sStr2)

sStr1 = 'strchr'

sStr2 = 'strch'

print cmp (sStr1, sStr2)

'''5. Scan it contains a specified character string'''

# Strspn (sStr1, sStr2)

sStr1 = '12345678 '

sStr2 = '456 '

# SStr1 and chars both in sStr1 and sStr2

print len (sStr1 and sStr2)

'''6. String length'''

# Strlen (sStr1)

sStr1 = 'strlen'

print len (sStr1)

'''7. The string lowercase characters converted to uppercase characters'''

# Strlwr (sStr1)

sStr1 = 'JCstrlwr'

sStr1 = sStr1.upper ()

print sStr1

'''8. Additional specified length of the string'''

# Strncat (sStr1, sStr2, n)

sStr1 = '12345 '

sStr2 = 'abcdef'

n = 3

sStr1 + = sStr2 [0: n]

print sStr1

'9''. Comparative specified length string'''

# Strncmp (sStr1, sStr2, n)

sStr1 = '12345 '

sStr2 = '123bc '

n = 3

print cmp (sStr1 [0: n], sStr2 [0: n])

'''10. Copy a specified length of characters'''

# Strncpy (sStr1, sStr2, n)

sStr1 =''

sStr2 = '12345 '

n = 3

sStr1 = sStr2 [0: n]

print sStr1

'''11. String comparison, case insensitive'''

# Stricmp (sStr1, sStr2)

sStr1 = 'abcefg'

sStr2 = 'ABCEFG'

print cmp (sStr1.upper (), sStr2.upper ())

'''12. N characters before the string replaced with the specified character'''

# Strnset (sStr1, ch, n)

sStr1 = '12345 '

ch = 'r'

n = 3

sStr1 = n * ch + sStr1 [3:]

print sStr1

'''13. Scanning the string'''

# Strpbrk (sStr1, sStr2)

sStr1 = 'cekjgdklab'

sStr2 = 'gka'

nPos = -1

for c in sStr1:

if c in sStr2:

nPos = sStr1.index (c)

break

print nPos

'''14. Turn a string'''

# Strrev (sStr1)

sStr1 = 'abcdefg'

sStr1 = sStr1 [:: -1]

print sStr1

'''15. Find a string'''

# Strstr (sStr1, sStr2)

sStr1 = 'abcdefg'

sStr2 = 'cde'

print sStr1.find (sStr2)

'''16. Segmentation string'''

# Strtok (sStr1, sStr2)

sStr1 = 'ab, cde, fgh, ijk'

sStr2 = ','

sStr1 = sStr1 [sStr1.find (sStr2) + 1:]

print sStr1

相关文章
  • learning python string 2010-08-22

    # Python string operations '''1. Copy the string''' # Strcpy (sStr1, sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2 '''2. Connection string''' # Strcat (sStr1, sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 + = sStr2 print sStr1

  • Python string templates 2010-04-20

    Python string templates, you can type without the need to remember the details. subsitute () # strict mode, the key missing will be reported under the exception of a keyError safe_subsitute () # in the key case of the missing will output the string i

  • python string where you might not notice 2010-05-31

    python string concatenation There are several ways, I began to use the first method the efficiency is the lowest, then after reading the book back on the use of the Two kinds of efficient methods, we want to help. First introduced to the low efficien

  • python string replace the 2 methods 2010-05-31

    python string replace is the python string operations often encounter problems when, where, under a brief string replace method. python string replacement can be achieved in 2 ways: 1 is the method with the string itself. 2 to replace the string with

  • useful sites for learning python 2010-11-26

    Python Language Study == Overview == . For a programming language, syntax is the most intuitive performance, a variety of class library API is the most direct application. . But under the surface, the infrastructure of language, models, principles an

  • Get the hands-on fun learning python web price data super tycoon 2010-02-23

    Recently while learning while doing this thing, cause is happy to play around the net people are super tycoon, which price data is updated once every 10 minutes, Internet search of the search, and then started to do. 1, Web log At first I did not fin

  • python string parsing, using string and regular expression module re module 2010-03-15

    1.python strings usually double quotes single quotes ('...'), ("..."), three quotes ("""...""") or ('''...'' ') surrounded by a string of three quotes included multi-line composition, and can be said that a large se

  • Introduction to learning --- python reproduced hour 2010-04-22

    A preface for the reader this article as soon as possible for experienced programmers into the Python2.x world. In particular, if you learn Java and Javascript, not one hour you can use Python to write smoothly useful quick Python program. ----------

  • python string operations (connections. more. formatting, etc.) 2010-05-25

    <br /> A string concatenation method: >>> str1 = 'hello' >>> str2 = 'world' >>> str1_2 = str1 + ' ' + str2 >>> str1_2 'hello world' >>> print str1_2 hello world >>> Method 2: >>> str12 =

  • Learning Python module - using optparse command-line argument processing 2010-10-29

    Python has two built-in module for processing command line arguments: One is the getopt, "Deep in python" is also mentioned in the book, only simple processing command line arguments; Another is the optparse, It is powerful and easy to use, you

  • python string processing 2011-01-06

    #! / Usr / bin / python # -*- Coding: utf-8 -*- from string import * import sys import traceback if __name__ == '__main__': str = "gaBriel woNg" chn = "Wang Zhongpei" # "Gabriel" and "Wong" is a tab character betwee

  • python string processing 2 2011-01-06

    String types in Python usage. This part I is to collect some ideas. Internet has a lot of friends to share the use of Python in the String type. String form of expression 1: 'me' single quote 2: "me" the way the first two quotes match, very Java

  • Ruby Learning 1 - String 2011-05-06

    , Slice: silce, []-----------------[] is silce alias, so the two are exactly the same operation 1: to determine whether the string contains the string / sub-mode string [substring] string [/ pattern /] string [/ pattern /, position] # position after

  • python string methods 和 string module区别 2012-12-05

    python的模块真的很乱,版本之间变化多且有点随意,远没有java那么严谨,造就了它的灵活又无标准. 如string模块,从python 1.6 开始,此模块就少用了,大多函数已由标准str对象实现了. 但string的某些属性又没有被替代,如string.ascii_letters之类的,str并没有对应的替代属性. 原本不是面向对象的python,慢慢向对象靠拢,纠结. 以下内容是转帖 ----------------------------------------------------

  • Python string encode and decode 2010-02-02

    The role is to decode the other code string into a unicode encoding, such as str1.decode ('gb2312'), said it would convert gb2312 unicode encoded string encoding. The role is to encode unicode encoding into a string of other codes, such as str2.encod

  • Learning Python Record - new to the road stickers 2010-04-17

    Record mess things ------------------------------- Core Python writes File is a continuous sequence of bytes

  • Learning python for a little knowledge 2010-07-17

    There is a long time wanted to write python iteration related things, but did not know how to express so that we can easily understand. Today I would like for a very long time, a little sort out the next idea, using simple language and we talk about.

  • Learning Python django 2010-08-31

    1 Python installed: Setting windows path path = D: \ python Ensure that the correct python to run. 2 download Django, run python setup.py install 3 run Django First establish a project django-admin.py startproject mysite 4 mysite project to establish

  • Learning python pptv to advertising script 2010-09-02

    #! / Usr / local / env python # -*- Coding = utf-8 -*- import os import stat fileConfigList = ( r "\ PPLive \ PPTV \ xml \ framegallop.xml", r "\ PPLive \ PPTV \ xml \ playctrlgallop.xml", r "\ PPLive \ PPTV \ cache \ pluginad \ A

  • Started learning python 2010-09-27

    Materials in accordance with the following 1 day of time spent learning a bit http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html