python字符串相关练习

版权声明:===========版权所有,可随意转载,欢迎互相交流=========== https://blog.csdn.net/weixin_42446330/article/details/81486485

练习一:

输入一个字符串 s, 返回 
由字符串的最前面两个字母和最后两个字母组成的字符串。
例如: 'spring' 返回 'spng', 'is' 返回 'is'
当输入的字符串长度小于2时,返回空字符串

def both_ends(s):
    if len(s)<2:
        print ''
    elif len(s)==2:
        print(s)
    else:
        print(s[0]+s[-1])
    return
both_ends('afasdfa')

输出结果:

练习二:

输入一个字符串s, 返回满足以下条件的字符串
1.找出与字符串的第一个字母相同的字母,把它们替换成 '*',除了第一个字母本身以外
例如: 输入'babble', 返回 'ba**le'
提示:使用 s.replace(stra, strb) 函数,可以将字符串 s 中的所有 子字符串stra 替换为 子字符串strb

def fix_start(s):
    f1=s[0]
    b1=s[1:]
    fix_back = b1.replace(f1,'*')
    return f1+fix_back
print fix_start('babble')

输出结果:

练习三:

输入字符串 a 和 b, 返回添加以下条件的字符串
1.使用空格把两个字符串分隔后合并成一个字符串
2.交换两个字符串的最前面的两个字母
3.字符串 a 和 b 的长度都大等于2
例如: 'mix, pod' -> 'pox mid'
  'dog', 'dinner' -> 'dig donner'

def mix_up(a, b):
    m1 = a[0]+b[1]+a[2:]
    m2 = b[0]+a[1]+b[2:]
    return m1+m2

print mix_up('mix','pod')
print mix_up('dog','dinner')

输出结果;

练习四:

Given a string, find the first appearance of the
substring 'not' and 'bad'. If the 'bad' follows
the 'not', replace the whole 'not'...'bad' substring
with 'good'.
Return the resulting string.
So 'This dinner is not that bad!' yields:
This dinner is good!

def not_bad(s):
    f=s.find('not')
    b=s.find('bad')
    if f !=-1 and b!=-1 and b>f:
        s = s[:f]+'good'+s[b+3:]
    return s

print not_bad('This dinner is not that bad!')

输出结果:

练习五;

Consider dividing a string into two halves.
If the length is even, the front and back halves are the same length.
If the length is odd, we'll say that the extra char goes in the front half.
e.g. 'abcde', the front half is 'abc', the back half 'de'.
Given 2 strings, a and b, return a string of the form
 a-front + b-front + a-back + b-back

def front_back(a, b):
  # +++your code here+++
  # LAB(begin solution)
  # Figure out the middle position of each string.
  a_middle = int(len(a) / 2)
  b_middle = int(len(b) / 2)
  if len(a) % 2 == 1:  # add 1 if length is odd
    a_middle = a_middle + 1
  if len(b) % 2 == 1:
    b_middle = b_middle + 1
  return a[:a_middle] + b[:b_middle] + a[a_middle:] + b[b_middle:]

print front_back('abcde','high')

输出结果:

练习六:

输入一个字符串列表,
返回同时满足以下两个条件的字符串的个数:
1.字符串长度大等于2
2.字符串的第一个字符等于最后一个字符
注:python语言中没有 ++ 操作符,但是有 += 操作符。
def match_ends(words):
    n = 0
    for i in words:
        new_words=[]
        if i >=2 and i[0]==i[-1]:
            new_words.append(i)
            n+=1
        else:
            pass
    print n
    return new_words
print match_ends(['asdas','dfsafs','ertdsjfhj','fsdsf'])

输出结果;

练习七:

输入一个字符串列表,
返回满足以下条件的字符串列表:
1.按字母顺序从小到大排序
2.第一个字母是'x'的字符串排列在最前面
例如:输入 ['mix', 'xyz', 'apple', 'xanadu', 'aardvark']
      返回 ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
提示:可以通过生成两个列表并对它们分别进行排序,然后再把它们连接起来。
def front_x(words):
    # +++your code here+++
    x_words=[]
    other_words = []
    for w in words:
        if w[0]=='x':
            x_words.append(w)
        else:
            other_words.append(w)
    return x_words + sorted(other_words)

print front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark'])

输出结果:

练习八:

D. Given a list of numbers, return a list where
all adjacent == elements have been reduced to a single element,
so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or
modify the passed in list.

def remove_adjacent(nums):
    result = []
    for i in nums:
        if len(result) == 0 or i != result[-1]:
            result.append(i)
    return result
print remove_adjacent([1, 2, 2, 3])

输出结果:

猜你喜欢

转载自blog.csdn.net/weixin_42446330/article/details/81486485