回覆列表
  • 1 # 使用者9231804048218

    以python語言為例,示例程式碼如下

    #!/usr/bin/env python

    # -*- coding: utf-8 -*-

    # 編一個程式,輸入兩個字串str1和str2,計算str2在str1中出現的位置,輸出位置結果

    str1 = raw_input()

    str2 = raw_input()

    if str2 in str1:

    print len(str1.split(str2)[0]) + 1

    執行程式,分別輸入 strastrb,strb,則輸出結果為5。表示strb是從strastrb的第5個索引位置的。

    https://iknow-pic.cdn.bcebos.com/8b13632762d0f70391c6bf3406fa513d2797c5e5

    擴充套件資料

    python raw_input() 函式介紹:

    raw_input()用來獲取控制檯的輸入,將所有輸入作為字串看待,返回字串型別。注意:

    例項:

    >>>a = raw_input("input:")

    input:123

    >>> type(a)

    >>> a = raw_input("input:")

    input:runoob

    >>> type(a)

  • 中秋節和大豐收的關聯?
  • 為什麼現在的人熱衷於養寵物,什麼寵物最受歡迎?